Java 无法获得客户端或服务器响应
Java can't get client or server response
我来自波兰,如有任何错误,我深表歉意。
当我遇到烦人的问题时,我已经编写了一段时间的小型服务器和客户端连接。当我从客户端向服务器发送信息(字符串)时,服务器可以获取它,但无法响应它。
这是代码。
客户端
private static Socket socket;
public static void main(String args[]){
try{
String host = "localhost";
int port = 25002;
InetAddress address = InetAddress.getByName(host);
socket = new Socket();
socket.connect(new InetSocketAddress(host, port), 5000);
//Send the message to the server
System.out.println("< "+sendMessage(socket));
//socket.shutdownOutput();
System.out.println("> "+getMessage(socket));
}catch (SocketTimeoutException e){
System.out.println(e.getMessage()); // changed
}catch (IOException e){
System.out.println(e.getMessage()); // changed
}catch (IllegalBlockingModeException e){
System.out.println(e.getMessage()); // changed
}catch(IllegalArgumentException e){
System.out.println(e.getMessage()); // changed
}finally{
//Closing the socket
try{
socket.close();
}catch(Exception e){
System.out.println(e.getMessage()); // changed
}
}
}
public static String sendMessage(Socket client){
try {
String message = "test";
PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
writer = new PrintWriter(client.getOutputStream(), true);
writer.print(message);
writer.flush();
writer.close();
return message;
} catch (IOException e) {
System.out.println(e.getMessage()); // changed
return "false";
}
}
public static String getMessage(Socket client){
try {
BufferedReader socketReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
return socketReader.readLine();
} catch (IOException e){
System.out.println(e.getMessage()); // changed
return "false";
}
}
还有..服务器
public class kRcon{
private static Socket socket;
private static ServerSocket serverSocket;
private static Thread u;
private static class Server extends Thread {
public void run() {
int port = 25002;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true) {
try {
socket = serverSocket.accept();
BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String str = socketReader.readLine();
socketReader.close();
System.out.println(str);
socketWriter.write("test");
socketWriter.flush();
socketWriter.close();
}
}catch (IOException e1) {
e1.printStackTrace();
}
}
public static void init(){
try {
u = new Server();
u.setName("Server");
u.start();
} catch (IOException e) {
System.out.println(e.getMessage()); // changed
}
}
}
结果
如果,我首先启动服务器,一切看起来都不错。
所以,我用参数 "test" 启动客户端,控制台的输出是:
< test
Socket is closed // changed
在控制台的服务器端我有:
"test"
Socket is closed // changed
我尝试关闭输入和输出,但没有用。我现在不知道该怎么做。请帮忙:c
2015-04-03 编辑
我用注释 "changed" 更改了行。
对于Google和读者
要解决问题,请不要关闭客户端的 StreamReaders 和 StreamWriters。
感谢EJP,求助!
来自波兰的问候。
当你得到异常时,打印它。不要只是丢弃所有这些信息。也不要 return 魔法 Strings
。在这种情况下,您应该让异常传播。如果您完成了所有这些,您会看到异常 SocketException: socket closed
被 getMessage(),
抛出,并且您将有一些具体的事情需要调查,而不是一个完整的谜。
这是由于在 sendMessage().
中关闭 PrintWriter
造成的。关闭套接字的输入或输出流会关闭另一个流和套接字。
我来自波兰,如有任何错误,我深表歉意。
当我遇到烦人的问题时,我已经编写了一段时间的小型服务器和客户端连接。当我从客户端向服务器发送信息(字符串)时,服务器可以获取它,但无法响应它。
这是代码。
客户端
private static Socket socket;
public static void main(String args[]){
try{
String host = "localhost";
int port = 25002;
InetAddress address = InetAddress.getByName(host);
socket = new Socket();
socket.connect(new InetSocketAddress(host, port), 5000);
//Send the message to the server
System.out.println("< "+sendMessage(socket));
//socket.shutdownOutput();
System.out.println("> "+getMessage(socket));
}catch (SocketTimeoutException e){
System.out.println(e.getMessage()); // changed
}catch (IOException e){
System.out.println(e.getMessage()); // changed
}catch (IllegalBlockingModeException e){
System.out.println(e.getMessage()); // changed
}catch(IllegalArgumentException e){
System.out.println(e.getMessage()); // changed
}finally{
//Closing the socket
try{
socket.close();
}catch(Exception e){
System.out.println(e.getMessage()); // changed
}
}
}
public static String sendMessage(Socket client){
try {
String message = "test";
PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
writer = new PrintWriter(client.getOutputStream(), true);
writer.print(message);
writer.flush();
writer.close();
return message;
} catch (IOException e) {
System.out.println(e.getMessage()); // changed
return "false";
}
}
public static String getMessage(Socket client){
try {
BufferedReader socketReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
return socketReader.readLine();
} catch (IOException e){
System.out.println(e.getMessage()); // changed
return "false";
}
}
还有..服务器
public class kRcon{
private static Socket socket;
private static ServerSocket serverSocket;
private static Thread u;
private static class Server extends Thread {
public void run() {
int port = 25002;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true) {
try {
socket = serverSocket.accept();
BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String str = socketReader.readLine();
socketReader.close();
System.out.println(str);
socketWriter.write("test");
socketWriter.flush();
socketWriter.close();
}
}catch (IOException e1) {
e1.printStackTrace();
}
}
public static void init(){
try {
u = new Server();
u.setName("Server");
u.start();
} catch (IOException e) {
System.out.println(e.getMessage()); // changed
}
}
}
结果
如果,我首先启动服务器,一切看起来都不错。
所以,我用参数 "test" 启动客户端,控制台的输出是:
< test
Socket is closed // changed
在控制台的服务器端我有:
"test"
Socket is closed // changed
我尝试关闭输入和输出,但没有用。我现在不知道该怎么做。请帮忙:c
2015-04-03 编辑
我用注释 "changed" 更改了行。
对于Google和读者
要解决问题,请不要关闭客户端的 StreamReaders 和 StreamWriters。
感谢EJP,求助!
来自波兰的问候。
当你得到异常时,打印它。不要只是丢弃所有这些信息。也不要 return 魔法 Strings
。在这种情况下,您应该让异常传播。如果您完成了所有这些,您会看到异常 SocketException: socket closed
被 getMessage(),
抛出,并且您将有一些具体的事情需要调查,而不是一个完整的谜。
这是由于在 sendMessage().
中关闭 PrintWriter
造成的。关闭套接字的输入或输出流会关闭另一个流和套接字。