111,098
社区成员




private void CloseClientSocket(SocketAsyncEventArgs e)
{
AsyncUserToken token = e.UserToken as AsyncUserToken;
// close the socket associated with the client
try
{
token.Socket.Shutdown(SocketShutdown.Send);
token.Socket.Close();
}
// throws if client process has already closed
catch (Exception) { }
finally // <----
{
// decrement the counter keeping track of the total number of clients connected to the server
Interlocked.Decrement(ref m_numConnectedSockets);
// Free the SocketAsyncEventArg so they can be reused by another client
m_readWritePool.Push(e);
m_maxNumberAcceptedClients.Release();
}
Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", m_numConnectedSockets);
}
if (e.AcceptSocket.RemoteEndPoint == null)
{
m_maxNumberAcceptedClients.Release();
StartAccept(e);
return;
}
试了几次似乎没什么问题.....我没怎么搞明白当一个异步连接触发时为什么有时候RemoteEndPoint是null,这种情况出现在客户端同时连接数大于服务端监听时。然后同时关闭全部客户端时会出现RemoteEndPoint是null