Java 使用连接到客户端的ObjectInputStream和ObjectOutputStream从客户端读取数据

Java 使用连接到客户端的ObjectInputStream和ObjectOutputStream从客户端读取数据,java,sockets,networking,objectoutputstream,objectinputstream,Java,Sockets,Networking,Objectoutputstream,Objectinputstream,我的代码与下面的代码类似。我遇到的问题是,当ObjectInputStream(输入变量)尝试读取对象时(只有在建立连接后才会发生),它会崩溃并给我一个错误: java.net.SocketException:连接重置 并指具有以下特征的线路: message=(字符串)input.readObject() 它进入ex exception循环,而只有在客户端断开连接时(据我所知),它才应该进入ex exception循环 谢谢大家! 更新:我知道了。我最初尝试了EOFEException,但由于

我的代码与下面的代码类似。我遇到的问题是,当ObjectInputStream(输入变量)尝试读取对象时(只有在建立连接后才会发生),它会崩溃并给我一个错误:

java.net.SocketException:连接重置

并指具有以下特征的线路:

message=(字符串)input.readObject()

它进入ex exception循环,而只有在客户端断开连接时(据我所知),它才应该进入ex exception循环

谢谢大家!

更新:我知道了。我最初尝试了EOFEException,但由于某种原因,它从未被调用。我最终发现,我的客户端存在一个问题,它实际上没有发送任何数据,并在运行时立即断开连接。对于遇到与我相同问题的任何人,以下是我的固定代码:

    /** 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

可能有点多余和不必要(我真的只需要其中一个),但知道有两件事可以依靠我会感觉更好


希望这有帮助

“连接重置”有许多可能的原因,但最常见的原因是您写入了已被对等方关闭的连接:换句话说,是应用程序协议错误


您的循环应该单独捕获
EOFEException
,并将其视为有序关闭。

“我有类似于下面代码的代码”要更快获得更好的帮助,请发布一个。
catch(异常noConnectionToSocketFound){//如果客户端不再与服务器通信,将被调用
:这是对一段糟糕代码的误导性评论。您应该单独捕获
EOFEException
,记录它,然后关闭连接;然后,您应该捕获与您在这一行上的评论相当接近的
IOException,
f代码。捕获
异常
通常是一种糟糕的做法。@EJP我尝试了您所说的,但即使在客户端断开连接时,它也从未被调用。如果您能说明我将如何实现此功能,我将不胜感激,因为我无法理解。
    /** 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;