0

I am designing a Client Server Chat application in Java which uses TCP connection between them. I am not able to figure out how to detect at server side when a client forcefully closes down. I need this as i am maintaining a list of online clients and i need to remove user from the list when he forcefully closes the connection.

Any help will be highly appreciated.

Thanks

Tara Singh

Kylar
  • 8,876
  • 8
  • 41
  • 75
Tara Singh
  • 1,821
  • 6
  • 28
  • 36
  • Did you find any of these helpful? Please return to your questions and keep them updated. – Kylar Apr 22 '10 at 17:37

3 Answers3

2

One way to receive timely notification of a disconnect is to attempt to send a small piece of information at regular intervals. Then, the latest that you'll know of a client disconnect is at most your interval. People call this a heartbeat.

Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
1

Assuming that your server is using a java.net.Socket, you can query the socket from time to time, it provides methods isClosed() and isConnected().

Kylar
  • 8,876
  • 8
  • 41
  • 75
0

It depends on how you're handling your socket I/O

For example, if you're using a selector (java.nio) to do non-blocking I/O on a set of sockets you're going to find out about any disconnects the next time you call select().

Maybe if you updated your question with how you're handling the sockets?

Brian Roach
  • 76,169
  • 12
  • 136
  • 161