File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Best way to check if client socket has disconnected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Best way to check if client socket has disconnected?" Watch "Best way to check if client socket has disconnected?" New topic
Author

Best way to check if client socket has disconnected?

Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
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.


Dominic Steng�rd<br />Sun Certified Java 2 Programmer
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Don't get me started about those stupid light bulbs.
 
subject: Best way to check if client socket has disconnected?