NetCoreServer icon indicating copy to clipboard operation
NetCoreServer copied to clipboard

How to check if TcpClient is NOT Connected?

Open LeoYoung opened this issue 3 years ago • 1 comments

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?

LeoYoung avatar Jul 15 '20 06:07 LeoYoung

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

posilva avatar Sep 09 '20 10:09 posilva