///
/// 检查一个Socket是否可连接
///
///
///
private bool IsConnected(Socket socket)
{
if (socket == null || socket.Connected == false)
{
return false;
}
bool blockingState = socket.Blocking;
try
{
byte[] tmp = new byte[];
socket.Blocking = false;
socket.Send(tmp, , );
return true;
}
catch (SocketException e)
{
// 产生 10035 == WSAEWOULDBLOCK 错误,说明被阻止了,但是还是连接的
if (e.NativeErrorCode.Equals())
{
return true;
}
else
{
return false;
}
}
finally
{
socket.Blocking = blockingState; // 恢复状态
}
}
上面的代码来自互联网
原理就是在外面加个timer/task/thread