簡體   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