(4)close后续行为
来源:互联网 发布:淘宝达人申请验证方法 编辑:程序博客网 时间:2024/04/28 04:07
外国人写的关于TIME_WAIT讲的最好的文章
TIME_WAIT概念
TIME_WAIT的产生条件
主动关闭方在发送四次挥手的最后一个ACK会变为TIME_WAIT状态,保留次状态的时间为两个MSL(linux里一个MSL为30s,是不可配置的)
TIME_WAIT两个MSL的作用
可靠安全的关闭TCP连接。比如网络拥塞,主动方最后一个ACK被动方没收到,这时被动方会对FIN开启TCP重传,发送多个FIN包,在这时尚未关闭的TIME_WAIT就会把这些尾巴问题处理掉,不至于对新连接及其它服务产生影响。
TIME_WAIT占用的资源
少量内存(查资料大概4K)和一个fd。
TIME_WAIT关闭的危害
1-网络情况不好时,如果主动方无TIME_WAIT等待,关闭前个连接后,主动方与被动方又建立起新的TCP连接,这时被动方重传或延时过来的FIN包过来后会直接影响新的TCP连接;2-同样网络情况不好并且无TIME_WAIT等待,关闭连接后无新连接,当接收到被动方重传或延迟的FIN包后,会给被动方回一个RST包,可能会影响被动方其它的服务连接。
TIME_WAIT相关系统参数
注:tcp_timestamps默认是开启的的,如果还开启tcp_tw_recycle,60s(timewai时间)内同一源ip主机的socket connect请求中的timestamp必须是递增的。对于服务端,同一个源ip可能会是NAT后很多机器,这些机器timestamp递增性无可保证,服务器会拒绝非递增请求连接,直接导致不能三次握手。
注:tcp_tw_reuse只对主动发起连接的那方有效。
SO_REUSEADDR
Indicates that the rules used in validating addresses supplied in a bind(2) call should allow reuse of local addresses. For AF_INET sockets this means that a socket may bind, except when there is an active listening socket bound to the address. When the listening socket is bound to INADDR_ANY with a specific port then it is not possible to bind to this port for any local address. Argument is an integer boolean flag。
在开启重用的情况下,只有ip:port完全相同才认为是相同的地址;没有开启的话,只要端口被占用就出错。所以这个在端口相同的情况下,针对ip(也就是addr)而言的。只有使用bind的时候才考虑这个玩意,因为可能存在TIME_WAIT的连接导致bind出错。
SO_REUSEPORT (since Linux 3.9)
Permits multiple AF_INET or AF_INET6 sockets to be bound to an identical socket address. This option must be set on each socket (including the first socket) prior to calling bind(2) on the socket. To prevent port hijacking, all of the processes binding to the same address must have the same effective UID. This option can be employed with both TCP and UDP sockets.
For TCP sockets, this option allows accept(2) load distribution in a multi-threaded server to be improved by using a distinct listener socket for each thread. This provides improved load distribution as compared to traditional techniques suchusing a single accept(2)ing thread that distributes connections, or having multiple threads that compete to accept(2) from the same socket.
For UDP sockets, the use of this option can provide betterdistribution of incoming datagrams to multiple processes (or threads) as compared to the traditional technique of having multiple processes compete to receive datagrams on the same socket.
- (4)close后续行为
- 后续
- 后续
- close ()
- close()
- 行为4
- Java开发中的23种设计模式详解_后续2_行为型模式
- 2.5.4拖放行为
- 行为
- 行为
- BufferedReader.close() && FileReader.close()
- [原创]WCF后续之旅(4):WCF Extension Point 概览
- WCF后续之旅(4):WCF Extension Point 概览
- 出卖(后续)
- 端午节后续
- 关于后续...
- 后续系列
- PAM后续
- 快速排序
- mysql主从复制配置
- LeetCode基础--动态规划
- zookeeper的选主机制的实现过程以及原理
- 每天一个linux命令(28):tar命令
- (4)close后续行为
- 【Linux】基础与命令一
- iframe导航栏切换高亮(selected)
- android studio 3.0 跨版本应用内升级问题解决(检查升级只有download按钮,无Update and restart按钮)
- Android studio 问题处理记录
- 关于一些解决客户订单的问题
- POJ1631 Bridging signals(利用STL的lower_bound快速求最长上升子序列)
- 认识并编写一个Makefile
- Piggy-Bank