How to detect a remote side socket close?

JavaNetworkingSocketsTcp

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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.");
}

Solution 4 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin WongView Question on Stackoverflow
Solution 1 - JavaWMRView Answer on Stackoverflow
Solution 2 - JavaThorsten NiehuesView Answer on Stackoverflow
Solution 3 - JavaSangameshView Answer on Stackoverflow
Solution 4 - JavaDumbestGuyOnSaturnView Answer on Stackoverflow