【发布时间】:2013-02-04 23:44:00
【问题描述】:
我的代码类似于下面的代码。我遇到的问题是,当 ObjectInputStream(输入变量)尝试读取一个对象(这只会在建立连接后发生)时,它会崩溃并给我一个错误:
java.net.SocketException:连接重置
并且指的是带有:
的行message = (String) input.readObject();
只有在客户端断开连接时才应该进入 ex 异常循环(据我所知)。
@Override
public void run() {
String message = "";
do{
try{
message = (String) input.readObject();
}catch(ClassNotFoundException cnfException){
System.err.println("Unable to read client data.");
}catch(Exception ex){ // Will get called if the client is no longer in communication with the server.
continueTalking = false; // If the client disconnects, the server will shut down its communication with the client.
ex.printStackTrace();
break; // Breaks out of the do...while loop.
}
if(message.equals("DISCONNECT")){
continueTalking = false;
System.out.println(connection.getLocalAddress() + " disconnected from the session.");
}
else{
//TODO Send the message off to be processed.
}
}while(continueTalking); // Continues waiting for a message until continueTalking = false
closeStreams();
}
谢谢!
更新:我想通了。我最初尝试过 EOFException,但由于某种原因从未调用过。我最终发现这是我的客户端的一个问题,它实际上并没有发送任何数据并且一运行就自行断开连接。对于和我遇到同样问题的人,这是我的固定代码:
/** This should handle the connection **/
@Override
public void run() {
/** The message that the client has sent to the server. **/
String message = "";
do{
try{
message = (String) input.readObject(); // Waits for the client to send a message to the server.
}catch(ClassNotFoundException cnfException){
System.err.println("Unable to read client data. Continuing on with my life.");
}catch(Exception noConnectionToSocketFound){ // Will get called if the client is no longer in communication with the server.
System.out.printf("User with handle: %s disconnected.\n", clientIP);
continueTalking = false; // If the client disconnects, the server will shut down its communication with the client.
break; // Breaks out of the do...while loop that is constantly waiting for the client to send a message to.
}
/** If the client sends the message "DISCONNECT", then the server will shut down all communications with said client. **/
if(message.equals("DISCONNECT")){
System.out.println(connection.getLocalAddress() + " disconnected from the session properly.");
continueTalking = false; // If the client wants to disconnect, then the server will stop trying to communication with said client.
break; // Breaks out of the do...while loop that is constantly waiting for the client to send a message to.
}
else if(message != ""){
System.out.printf("Got message from %s:\n%s\n", clientIP, message);
message = ""; // Is needed so that this loop doesn't get called every time after the first message has been sent. Without it, after the word "cheeseburger" is sent, it would continually think that "cheeseburger" is being repeatedly sent (this might not actually happen anymore).
//TODO Send the message off to be processed.
}
}while(continueTalking); // Continues waiting for a message until continueTalking = false
closeStreams(); // Just some clean up
continueTalking = false;
break;
可能有点多余和不必要(我真的只需要其中一个),但知道有两件事可以依靠我感觉更好。
希望这会有所帮助!
【问题讨论】:
-
“我的代码与下面的代码类似” 为了尽快获得更好的帮助,请发布SSCCE。
-
catch (Exception noConnectionToSocketFound){ // Will get called if the client is no longer in communication with the server:这是对一段糟糕代码的误导性评论。您应该单独捕获EOFException,记录它并关闭连接;然后你应该抓住IOException,,它与你对这行代码的评论相当接近。捕捉Exception通常是不好的做法。 -
@EJP 我试过你所说的,但即使客户端断开连接,它也没有被调用。如果您能阐明我将如何实现它以使其工作,将不胜感激,因为我只是想不通。
标签: java sockets networking objectoutputstream objectinputstream