How to detect a remote side socket close? [duplica

2019-01-01 15:42发布

问题:

This question already has an answer here:

  • Java socket API: How to tell if a connection has been closed? 6 answers

How do you detect if Socket#close() has been called on a socket on the remote side?

回答1:

The isConnected method won\'t help, it will return true even if the remote side has closed the socket. Try this:

public class MyServer {
    public static final int PORT = 12345;
    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(PORT);
        Socket s = ss.accept();
        Thread.sleep(5000);
        ss.close();
        s.close();
    }
}

public class MyClient {
    public static void main(String[] args) throws IOException, InterruptedException {
        Socket s = SocketFactory.getDefault().createSocket(\"localhost\", MyServer.PORT);
        System.out.println(\" connected: \" + s.isConnected());
        Thread.sleep(10000);
        System.out.println(\" connected: \" + s.isConnected());
    }
}

Start the server, start the client. You\'ll see that it prints \"connected: true\" twice, even though the socket is closed the second time.

The only way to really find out is by reading (you\'ll get -1 as return value) or writing (an IOException (broken pipe) will be thrown) on the associated Input/OutputStreams.



回答2:

Since the answers deviate I decided to test this and post the result - including the test example.

The server here just writes data to a client and does not expect any input.

The server:

ServerSocket serverSocket = new ServerSocket(4444);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
while (true) {
  out.println(\"output\");
  if (out.checkError()) System.out.println(\"ERROR writing data to socket !!!\");
  System.out.println(clientSocket.isConnected());
  System.out.println(clientSocket.getInputStream().read());
        // thread sleep ...
  // break condition , close sockets and the like ...
}
  • clientSocket.isConnected() returns always true once the client connects (and even after the disconnect) weird !!
  • getInputStream().read()
    • makes the thread wait for input as long as the client is connected and therefore makes your program not do anything - except if you get some input
    • returns -1 if the client disconnected
  • out.checkError() is true as soon as the client is disconnected so I recommend this


回答3:

You can also check for socket output stream error while writing to client socket.

out.println(output);
if(out.checkError())
{
    throw new Exception(\"Error transmitting data.\");
}


回答4:

The method Socket.Available will immediately throw a SocketException if the remote system has disconnected/closed the connection.