我正在使用以下代码创建一个异步 TCP 服务器:
private void SetupServerSocket()
{
var myEndpoint = new IPEndPoint(IPAddress.Any, _port);
_serverSocket = new Socket(myEndpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
_serverSocket.Bind(myEndpoint);
_serverSocket.Listen((int)SocketOptionName.MaxConnections);
}
protected void Open()
{
SetupServerSocket();
Opened = true;
_serverSocket.BeginAccept(AcceptCallback, _serverSocket);
}
private void AcceptCallback(IAsyncResult result)
{
var connection = new ConnectionInfo();
try
{
// Finish Accept
var s = (Socket)result.AsyncState;
connection.Socket = s.EndAccept(result);
connection.Buffer = new byte[8192];
lock (_connections)
{
_connections.Add(connection);
}
// Start Receive and a new Accept
connection.Socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, ReceiveCallback, connection);
_serverSocket.BeginAccept(AcceptCallback, result.AsyncState);
}
catch (SocketException ex)
{
CloseConnection(connection);
}
catch (Exception ex)
{
CloseConnection(connection);
}
}
private void CloseConnection(ConnectionInfo ci)
{
if (ci.Socket != null)
{
if (OnDisconnect != null)
OnDisconnect.Invoke((IPEndPoint)ci.Socket.RemoteEndPoint);
ci.Socket.Shutdown(SocketShutdown.Both);
ci.Socket.Close();
}
lock (_connections)
{
_connections.Remove(ci);
}
}
有些事情很难理解:
1 - 使用_serverSocket.SetSocketOption
我启用了在 TCP 中使用 KeepAlive。我发现默认情况下 Windows 的默认 KeepAlive Timer 为 2 小时!并且使用 Wireshark 确认了此行为。
经过一番谷歌搜索后,我在http://support.microsoft.com/kb/120642/EN-US中发现您可以通过在注册表中创建一个键来更改 Windows KeepAliveTime。我确实喜欢这个支持页面的说明,但是没有应用 KeepAlive 计时器(即使在重新启动后),它仍然是 2 小时。有谁知道如何在 Windows 中更改 KeepAlive 计时器?
2 - 代码connection.Socket = s.EndAccept(result)
可以抛出异常(通常是 SocketException)。为什么Socket.EndResult
扔SocketException
?
3 -如果它已经处于该状态,为什么我们必须_serverSocket
再次设置为接受状态?AcceptCallback()
谢谢