我写了一个简单的TCP客户端和服务器跑他们我注意到,当我使用 CTRL + C 来结束该程序TCP连接将被关闭但TCP连接状态就像 TIME_WAIT 那么,如果我再次运行TCP客户机和服务器TCP连接不能被再次建立一个 RST 包总是由TCP服务器发送我必须等待一段时间,直到下一个TCP连接建立
I wrote a simple tcp client and server and ran them I notice that when I use ctrl+c to end the program the tcp connection be be closed but the tcp connection state is like TIME_WAIT then if I run the tcp client and server again the tcp connection can't be established again a RST packet is always sent by the tcp server I have to wait for some time until the next tcp connection can be established
是什么,这和如何的原因来处理这个问题?
what is the reason for this and how to deal with this problem?
,我发现一台主机上,当一个TCP连接的状态为 FIN_WAIT_2 即使没有收到 FIN ,TCP连接将成为后就关闭了一段时间为什么?
besides, I notice on one host, when a tcp connection is in state FIN_WAIT_2 even if it doesn't receive FIN, the tcp connection will become closed some time later ,why?
感谢
推荐答案有关这个问题的解决办法是设置SO_REUSEADDR套接字选项。这告诉协议栈允许上碰巧处于TIME_WAIT状态地址绑定。
The solution for this problem is to set the SO_REUSEADDR socket option. This tells the stack to allow a bind on addresses that happen to be in the TIME_WAIT state.
原因TIME_WAIT状态似乎让一些时间失散的数据包到达。如果堆栈无法确保连接是正常关机,有可能在运输途中或发送数据包甚至可以主动发送更多数据。堆栈要避免这些数据包与新绑定连接的流量混合。
The reason for the TIME_WAIT state seems to be to allow some time for the straggler packets to arrive. If the stack cannot make sure that the connection was shutdown gracefully, there maybe packets in transit or the sender may even be sending more data actively. The stack wants to avoid mixing these packets with the traffic of a newly bound connection.
下面是关于使用SO_REUSEADDR的TCP和UDP一个很好的答案。
Here is a good answer on the use of SO_REUSEADDR for TCP and UDP.
更多推荐
当有符合国家'TCP连接不能建立TCP连接TIME
发布评论