0

我有类似于下面的代码的代码。我遇到的问题是,当ObjectInputStream(输入变量)试图读取一个对象(这将只发生在连接建立后)时,它崩溃并给我一个错误:使用连接到客户端的ObjectInputStream和ObjectOutputStream从客户端读取数据

java.net .SocketException:连接重置

并且是指与线:

消息=(字符串)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; 

可能是有点多余和不必要的(我只真正需要的其中之一),但我感觉更好,因为知道有两件事情要回退。

希望这会有所帮助!

+1

*“我的代码与下面的代码类似”*为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 – 2013-02-20 03:57:44

+0

'catch(Exception noConnectionToSocketFound){//如果客户端不再与服务器通信,将会被调用:这是对一段糟糕的代码的误导性评论。您应该单独捕获'EOFException',记录它并关闭连接;然后你应该赶上'IOException,'这与你对这行代码的评论非常接近。捕捉“例外”通常是不好的做法。 – EJP 2013-02-21 04:17:11

+0

@EJP我尝试了你所说的,但即使客户端断开连接,它也不会被调用。如果你能澄清我将如何实施这个工作,这将是非常感激,因为我无法弄清楚。 – 2013-02-21 05:42:04

回答

0

'连接重置'有很多可能的原因,但最常见的一种情况是您已写入已由对等关闭的连接:换句话说,应用程序协议错误。

你的循环应该分别捕获EOFException并将其视为有序关闭。