Jump to content
  • Advertisement
Sign in to follow this  
strahan

How do you determine if a client closed their connection

This topic is 4580 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

My server seems unable to detect if a client has closed or lost connection. My client is calling (in c#): NetworkStream.Close(); NetworkStream.Dispose(); TcpClient.Close() However, on the server side: TcpClient.Connected remains true. Also, TcpClient.GetStream() succeeds even those the stream was closed on the client side. What's the best way to determine if one of my client is gone? Thank you.

Share this post


Link to post
Share on other sites
Advertisement
I may be answering my own question (unless someone knows a better way) but after lots of Googling it seems the only way to know is if a read or write to the stream fails.

So, I added this hack:


private bool IsConnected(NetworkStream stream)
{
try
{
Byte[] bytes = new Byte[1];
stream.Read(bytes, 0, 0); // Read zero bytes.
}
catch
{
return false;
}
return true;
}


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!