Skip to content

Instantly share code, notes, and snippets.

@define-private-public
Created July 13, 2016 19:14
Show Gist options
  • Save define-private-public/2b28b8d087741f65ec602f95acb3474d to your computer and use it in GitHub Desktop.
Save define-private-public/2b28b8d087741f65ec602f95acb3474d to your computer and use it in GitHub Desktop.
Checks if a TcpClient has been disconnected.
// Checks if a socket has disconnected
// Adapted from -- http://stackoverflow.com/questions/722240/instantly-detect-client-disconnection-from-server-socket
private static bool _isDisconnected(TcpClient client)
{
try
{
Socket s = client.Client;
return s.Poll(10 * 1000, SelectMode.SelectRead) && (s.Available == 0);
}
catch(SocketException se)
{
// We got a socket error, assume it's disconnected
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment