我正在使用以下C#代码。
当我睡觉我的电脑并再次恢复时,Socket变为空。
private Socket m_sock = null;
我正在通过以下代码检查Socket。
if (m_sock == null) { return false; }
但是我很少得到假。睡眠/恢复计算机后,我一直都没有假。它返回false可能是在100(00)或更多周期之后。
所以,这里有更多的代码
internal virtual bool IsOpen
{
get
{
//If the socket equals null there is not a connection
if (m_sock == null) { return false; }
//return the sockets connected property
return m_sock.Connected;
}
}
m_sock = null在3个可能已知的地方:)
1
//If there is a socket object release it
if (m_sock != null) { m_sock = null; }
2
private void CloseSocket()
{
lock (lockObj)
{
if (m_sock == null) return;
if (m_sock.Connected)
{
m_sock.Shutdown(SocketShutdown.Both);
}
m_sock.Close();
m_sock = null;
}
}
3
//Check that there not already exists an open connection
if (!IsOpen)
{
//If there is a socket object release it
if (m_sock != null) { m_sock = null; }
}