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

时间:2013-02-20 03:48:11

标签: java sockets networking objectoutputstream objectinputstream

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

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

希望这有帮助!

1 个答案:

答案 0 :(得分:0)

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

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