当有符合国家'TCP连接不能建立TCP连接TIME_WAIT` [英] tcp connection can't be established when there is a tcp connection with state `TIME_WAIT`

查看:135
本文介绍了当有符合国家'TCP连接不能建立TCP连接TIME_WAIT`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的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_WAIT`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆