NetCoreServer
NetCoreServer copied to clipboard
How to check if TcpClient is NOT Connected?
I use NetCoreServer in my project. It is very easy to use and works well.
But I found a small problem: The Disconnected event not triggered in some situations. Such as plug off network cable, or server program crash down.
I use Socket.Poll()
to detect if the TcpClient Socket is disconnected
static class SocketExtensions
{
public static bool IsConnected(this Socket socket)
{
try
{
return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
}
catch (SocketException) { return false; }
}
}
But it doesn't work too. When I plugged off the cable, it also return true.
Is there any way to know the link is down?
I do not know if this helps but I tend to use this in my code: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socketasynceventargs.bytestransferred?view=netcore-3.1