Is it possible to check if the client socket is keeping connection alive at the moment? I use multithread server that use AsyncCallBack operations.
There is a lot of similar questions about this issue and I’ve read about Socket.Poll method – everything doesn’t work properly.
Tried to apply this sample:
bool IsConnectedAviable(Socket s)
{
try
{
if (s != null && s.Connected)
{
if (s.Poll(0, SelectMode.SelectRead))
{
byte[] buff = new byte[1];
if (s.Receive(buff, SocketFlags.Peek) == 0)
{
return false;
}
else
{
return true;
}
}
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
Thanks in advance! 🙂
If you receive something from the other end, you know the connection is still alive. If you don’t receive anything from the other end for a while, the only way to determine if the connection is still alive is by sending something.