Jump to content
  • Advertisement
Sign in to follow this  
HelloSkitty

Checking if a Connection Socket Is Dead

This topic is 2174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently have a Server program running with multiple sockets, which I'm testing with netcat. So I run the server, which makes a bunch of client handlers -- one for each client, and associates a socket with each. I have an infinite while loop running through and updating these handlers, and in the update code, I have this:


if (!socket.isConnected()) {
System.out.println("CONNECTION DROPPED");
server.handlers.remove(this);
}
if (socket.isInputShutdown()) {
System.out.println("INPUT SHUTDOWN");
}
if (socket.isOutputShutdown()) {
System.out.println("OUTPUT SHUTDOWN");
}
if (!socket.isBound()) {
System.out.println("NOT BOUND");
} //4 methods I found in the java doc for the Socket clas


So I have 4 if statements that presumably check if a socket is shut down. Unfortunately, when I kill one of my netcat test client instances, none of these if statements return a positive. Am I using these functions wrong, or is there some other function that achieves the purpose I seek?

I know that it must be possible to detect a dropped connection, as when I kill the server, all of my netcat clients know this and cease their function. I just can't seem to detect when a Client dies.

For those who aren't familiar with netcat, all it is is a simple program that can send messages across ports (at least, I think that's all it is).

Share this post


Link to post
Share on other sites
Advertisement
Generally speaking you have to attempt to do some IO on the socket before you can detect a dropped connection. Try setting it up so that your clients send a small "heartbeat" packet to the server every, say, 30 seconds. If you don't get a heartbeat for over a minute, you can assume the connection has dropped.

As a bonus, if your connection is closed normally, attempting IO will immediately return an error instead of forcing you to wait a time interval to detect a missing heartbeat.

Share this post


Link to post
Share on other sites
You could do what Apoch has mentioned, but you could also use the "heart beat" messages to display current latency towards the server.

If you are using TCP just attempt to read/write from the network, if a IO error returns/exception is thrown then you can safely assume the connection has closed. UDP wont work like that though as it is connectionless... Edited by Xanather

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!