Created
July 13, 2016 19:14
-
-
Save define-private-public/2b28b8d087741f65ec602f95acb3474d to your computer and use it in GitHub Desktop.
Checks if a TcpClient has been disconnected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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