From (Java Network Programming fourth edition):
To tell if a socket is currently open, you need to
check that isConnected()
returns true and isClosed()
returns false. For an example:
boolean connected = socket.isConnected() && ! socket.isClosed();
I need to find a way to discover as soon as possible that the client has disconnected from the Server Socket. Using the trick described above,I have tried the following :
Socket socket = ...;
while (socket.isConnected() && !socket.isClosed()) {
// do something ...
// here, the client is always connected
}
// client is disconnected
The above approach works for me, but it is always correct?. it detects all the cases?