Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to check if client socket has disconnected?

 
Dominic Steng�rd
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey javaguys!

Im programming a chat client and server. The server needs to know when the client has disconnected. I thought it would be enough for the server to check if the socket != null which I thought would occur when the client program was terminated, but this doesnt seem to work.

So which is the best way to check if the client has disconnected?
Thanks in advance.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java may have added some socket support in recent JDKs that provides some of the services available in native sockets. Not sure -- haven't kept up to date here.

However, the only way that I found that consistently works is to modify the protocol with a "heartbeat" that can be used to determine if the client is responding.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic