This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 Java 8 in Action this week in the Java 8 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: 18133
    
  39

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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to check if client socket has disconnected?
 
Similar Threads
Is Connectionalive (Urgent)
Reconnecting Sockets
socket programming on 2 ports
JUnit and Sockets
Servlet Refresh