// 检查一个Socket是否可连接
private bool IsSocketConnected(Socket client)
{
bool blockingState = client.Blocking;
try
{
byte[] tmp = new byte[1];
client.Blocking = false;
client.Send(tmp, 0, 0);
return false;
}
catch (SocketException e)
{
// 产生 10035 == WSAEWOULDBLOCK 错误,说明被阻止了,但是还是连接的
if (e.NativeErrorCode.Equals(10035))
return false;
else
return true;
}
finally
{
client.Blocking = blockingState; // 恢复状态
}
}
标签:false,Socket,检查,连接,client,return,Blocking
From: https://www.cnblogs.com/walterwhite/p/17749882.html