繁体   English   中英

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

[英]Read data from a client using ObjectInputStream and ObjectOutputStream connected to a client

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

java.net.SocketException:连接重置

并引用以下行:

消息=(String)input.readObject();

当它仅在客户端断开连接时才进入异常循环(据我所知)。

    @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;

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

希望这可以帮助!

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM