Tara Singh
Tara Singh

Reputation: 1841

Question regarding TCP Connection Forcefully shut down

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

Upvotes: 0

Views: 209

Answers (3)

Brian Roach
Brian Roach

Reputation: 76898

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?

Upvotes: 0

Kylar
Kylar

Reputation: 9334

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().

Upvotes: 1

Jonathon Faust
Jonathon Faust

Reputation: 12545

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.

Upvotes: 2

Related Questions