Socket使用经验
- 1. https://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open
- 2. socket.close()只是在app层面释放了相应资源,但是在 OS层面,这个socket资源并没有被释放,这个机制叫做「TcpTimedWaitDelay」。
1. https://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open
My C# .net application currently does the following (among other things):
Creates a thread which opens a socket on a specific port and waits for an instruction. Message comes in, socket thread reads the message and raises an event. An event handler calls the necessary functions to parse the message and performs the necessary action, for instance, launching an application. The specified external “application” launches, asynchronously. When my application restarts, but the external application does not close, the socket doesn’t seem to ever close. Then when I try to start up communication on that port again I get an error. However, as soon as I close the external application I am able to open a socket on that port.
It seems like my program is not closing down properly. It should be killing the socket when it exits, however when the external process is running that socket never closes.
简单来说,服务端根据client socket指令,打开了一个外部程序,但是 服务 process 关闭了,但是 服务打开的socket没有关闭。
This is just a stab because I have seen how you’re starting the external process. If you’re creating a Process object, my guess is you’re not setting:
ProcessStartInfo.UseShellExecute = true;
If UseShellExecute is set to false, the child process will inherit open socket handles from the parent process. This will keep your socket open even if the parent application is closed.
2. socket.close()只是在app层面释放了相应资源,但是在 OS层面,这个socket资源并没有被释放,这个机制叫做「TcpTimedWaitDelay」。
TcpTimedWaitDelay 是一个注册表设置项
HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
Data type | Range | Default value |
---|---|---|
REG_DWORD | 0x1E 0x12C ( 30–300 seconds ) | 0xF0 ( 240 seconds = 4 minutes ) |
注册表项说明 Description
Determines the time that must elapse(流逝) before TCP can release a closed connection and reuse its resources. This interval between closure and release is known as the TIME_WAIT state or 2MSL state. During this time, the connection can be reopened at much less cost to the client and server than establishing a new connection.
RFC 793 requires that TCP maintains a closed connection for an interval at least equal to twice the maximum segment lifetime (2MSL) (tcp标准要求实现者保留维护一个关闭的连接一段时间,这个时间最小是生存时间的两倍)of the network.
When a connection is released, its socket pair and TCP control block (TCB) can be used to support another connection.
By default, the MSL is defined to be 120 seconds, and the value of this entry is equal to two MSLs, or 4 minutes. However, you can use this entry to customize the interval.
Reducing the value of this entry allows TCP to release closed connections faster, providing more resources for new connections. However, if the value is too low, TCP might release connection resources before the connection is complete, requiring the server to use additional resources to reestablish the connection.
简言之,tcp connection close ——–> release, 中间需要经过一段时间。