簡體   English   中英

在Java中關閉客戶端時遇到異常

[英]Facing exception on closing client in Java

我是Java套接字編程的新手,因此面對問題似乎並不困難,但由於不熟悉而無法解決。 以下是客戶端和服務器的代碼。

服務器代碼:

public class connectionServer {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    String clientSentence;
    String capitalizedSentence;
    BufferedReader inFromClient;
    DataOutputStream outToClient;
    BufferedReader inFromUser;

    try {
        ServerSocket welcomeSocket = new ServerSocket(70);
        Socket connectionSocket;
        connectionSocket = welcomeSocket.accept();
        System.out.println("Connection accepted for " + connectionSocket.getInetAddress() + ": " + connectionSocket.getPort());
        InputStreamReader input = new InputStreamReader(connectionSocket.getInputStream());
        inFromClient = new BufferedReader(input);



        while (true) 
        {        

            if(input.ready())
            {
                clientSentence = inFromClient.readLine();                
                System.out.println(clientSentence);
            }

            String tempString = "FROM SERVER: What's problem......";

            try
            {
                Thread.currentThread().sleep(1000);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }

            outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            if(outToClient != null)
            {
                outToClient.writeBytes(tempString + "\n");
                outToClient.flush(); 

            }
        }
    } 
    catch (IOException e) 
    {
        System.out.println(e);
        e.printStackTrace();
    }
}

}

客戶代碼:

public class connectionClient {

static Socket clientSocket = null;
//////////////////////////////////////////

//////////////////////////////////////////
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    String sentence;
    String modifiedSentence;
    attachShutDownHook();
    try
    {
        clientSocket = new Socket("127.0.0.1", 70);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());;
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        outToServer.writeBytes("FROM CLIENT 2: Hello \n\n");

        while(clientSocket.isConnected())
        {               

            sentence = "Please reply me....";
            try
            {
                Thread.currentThread().sleep(1000);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
            if(inFromServer.ready())
            {
                modifiedSentence = inFromServer.readLine();
                System.out.println(modifiedSentence);
            }
            outToServer.writeBytes("FROM CLIENT 2:" + sentence + "\n");
            outToServer.flush();
        }

    }
    catch(IOException e)
    {
        System.out.println(e);
    }
    finally
    {
        System.exit (0) ;
    }
}

}

當我停止客戶端時,服務器端會引發以下異常

    java.net.SocketException: Connection reset by peer: socket write error
    java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:132)
at java.io.DataOutputStream.writeBytes(DataOutputStream.java:276)
at connectionserver.connectionServer.main(connectionServer.java:57)

在這方面的任何幫助將不勝感激。 謝謝

這通常意味着您已寫入已被對等方關閉的連接。 換句話說,應用程序協議錯誤。

您的代碼需要工作。

  1. 不要使用ready():僅阻塞read()直到數據到達。 目前,您正在吸煙CPU,而ready()返回false。 這可能是導致錯誤的原因。

  2. 連接套接字后,isConnected()始終為true。 當對等端關閉時,它不會神奇地返回false。 您必須通過EOS或異常檢測到。 在(isConnnected())上循環無效。

  3. 不要混淆信息流以及讀者和作家。 您正在使用BufferedInputStream:在另一端使用BufferedWriter。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM