views:

38

answers:

3

Hi All,

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

+2  A: 

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
Thank You for this suggestion. I did implement this.
Tara Singh
+1  A: 

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
A: 

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