0

我的代码类似于下面的代码。我遇到的问题是,当 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;

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

希望这可以帮助!

4

1 回答 1

0

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

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

于 2013-02-20T03:55:10.137 回答