13

Hey all. I have a server written in java using the ServerSocket and Socket classes.

I want to be able to detect and handle disconnects, and then reconnect a new client if necessary.

What is the proper procedure to detect client disconnections, close the socket, and then accept new clients?

Alex
  • 6,843
  • 10
  • 52
  • 71

3 Answers3

12

Presumably, you're reading from the socket, perhaps using a wrapper over the input stream, such as a BufferedReader. In this case, you can detect the end-of-stream when the corresponding read operation returns -1 (for raw read() calls), or null (for readLine() calls).

Certain operations will cause a SocketException when performed on a closed socket, which you will also need to deal with appropriately.

Rob
  • 47,999
  • 5
  • 74
  • 91
  • 1
    Where 'closed socket' means that *you* have closed the socket, not that the peer has closed the connection. All readXXX() methods other than those mentioned throw EOFException when EOSis encountered. – user207421 Aug 17 '13 at 10:17
4

The only safe way to detect the other end has gone is to send heartbeats periodically and have the other end to timeout based on a lack of a heartbeat.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Or you could just set the timeout – Pyrolistical Jan 28 '09 at 17:41
  • 2
    I have a thread which looks at each connection to see how long it has been since the last packet was read. (heartbeat or otherwise) It it has been too long, I close the connection. – Peter Lawrey Jan 29 '09 at 07:19
  • @PeterLawrey, sending heart beat is one of the best solution most of the Telecommunication system uses Heartbeat to respond to disconnect. – Alpesh Gediya May 27 '13 at 02:15
  • @AlpeshGediya I treat all messages as a heartbeat. This means you only need to send a heartbeat if nothing has been sent for a while. I have found this error prone under high load. i.e. you don't want to time-out a heartbeat because you have been reading some many intensive messages that the heartbeat took a long time before it is read. – Peter Lawrey May 29 '13 at 10:20
  • @PeterLawrey, Yes Under high volume traffic,This solution faces reality ... :) – Alpesh Gediya May 29 '13 at 10:23
-1

Is it just me, or has nobody noticed that the JavaDoc states a method under ServerSocket api, which allows us to obtain a boolean based on the closed state of the serversocket?

you can just loop every few seconds to check the state of it:

if(!serverSocket.isClosed()){
  // whatever you want to do if the serverSocket is connected
}else{
  // treat a disconnected serverSocket
}

EDIT: Just reading your question again, it seems that you require the server to just continually search for connections and if the client disconnects, it should be able to re-detect when the client attempts to re-connect. should'nt that just be your solution in the first place?

Have a server that is listening, once it picks up a client connection, it should pass it to a worker thread object and launch it to operate asynchronously. Then the server can just loop back to listening for new connections. If the client disconnects, the launched thread should die and when it reconnects, a new thread is launched again to handle the new connection.

Jenkov provides a great example of this implementation.

  • 4
    `isClosed()` doesn't tell you whether the peer has disconnected. It tells you whether *you* have closed *your socket*. – user207421 Dec 01 '16 at 23:47
  • 1
    And `ServerSocket.isClosed()` only tells you whether you have closed the *serversocket.* It has exactly nothing to do with clients of any kind. – user207421 Nov 13 '19 at 08:45