• Advertisement
Sign in to follow this  

How do you determine if a client closed their connection

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