Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the Socket TIME_WAIT problem

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What this article shares with you is about how to analyze Socket TIME_WAIT problems. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Socket TIME_WAIT problem

Tcp/ip detailed description of Volume 1 has a detailed introduction to the relevant content of tcp, in which TIME_WAIT is a bit obscure, today found a good article, special to share.

Netstat-n | awk'/ ^ tcp/ {+ + state [$NF]} END {for (key in state) print key, "/ t", state [key]}'

You will get results similar to the following, but the specific numbers will be different:

LAST_ACK 1

SYN_RECV 14

ESTABLISHED 79

FIN_WAIT1 28

FIN_WAIT2 3

CLOSING 5

TIME_WAIT 1669

Status: description

CLOSED: connectionless is active or in progress

LISTEN: the server is waiting for an incoming call

SYN_RECV: a connection request has arrived, waiting for confirmation

SYN_SENT: the application has started. Open a connection.

ESTABLISHED: normal data transfer statu

FIN_WAIT1: the application says it's done.

FIN_WAIT2: the other side has agreed to release

ITMED_WAIT: wait for all groups to die

CLOSING: both sides try to shut down at the same time

TIME_WAIT: the other side has initialized a release

LAST_ACK: wait for all groups to die

In other words, this command classifies and summarizes the network connection status of the current system.

Let's explain why it is written this way:

A simple pipe character connects the netstat and awk commands.

-

The longest time each TCP message can stay in the network is called MSL (Maximum Segment Lifetime), and its function is similar to the TTL of IP packets.

RFC793 points out that the value of MSL is 2 minutes, but in the actual implementation, there are three commonly used values: 30 seconds, 1 minute, and 2 minutes.

Note that when you enter the TIME_WAIT state, it is usually the client. Most servers generally perform passive shutdown and will not enter the TIME_WAIT state.

When a service is shut down and restarted on the server side, it will enter the TIME_WAIT state.

For example:

1. When the client connects to the 80 service of the server, the client will enable a local port to access 80 of the server, close the connection after the access is complete, and immediately access the server's

80, in which case the client will enable another local port instead of the one just used. The reason is that the connection is still in the TIME_WAIT state.

two。 When the client connects to the 80 service of the server, the server closes port 80 and immediately restarts the service at port 80. It may not start successfully because of the connection of the server.

The connection is still in the TIME_WAIT state.

Check the current value of net.ipv4.tcp_tw and change the current value to 1 minute:

[root@aaa1 ~] # sysctl-a | grep net.ipv4.tcp_tw

Net.ipv4.tcp_tw_reuse = 0

Net.ipv4.tcp_tw_recycle = 0

[root@aaa1 ~] #

Vi / etc/sysctl

Increase or modify the net.ipv4.tcp_ w value:

Net.ipv4.tcp_tw_reuse = 1

Net.ipv4.tcp_tw_recycle = 1

Enable kernel parameters to take effect:

[root@aaa1] # sysctl-p

[root@aaa1 ~] # sysctl-a | grep net.ipv4.tcp_tw

Net.ipv4.tcp_tw_reuse = 1

Net.ipv4.tcp_tw_recycle = 1

Observe normal again with netstat

The key to solving the problem here is how to reuse the value of time_wait. We can check the value of time and wait when we set it.

# sysctl-a | grep time | grep wait

Net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait = 120

Net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait = 60

Net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait = 120

Ask what's wrong with TIME_WAIT, is it idle and memory is not recycled?

Yes, this phenomenon is actually normal, and sometimes it has something to do with the large number of visitors. Set these two parameters: reuse indicates whether reapplication is allowed to be in the TIME-WAIT state.

Socket is used for new TCP connections; recyse is to speed up TIME-WAIT sockets recycling

Q: I'm writing a unix server program, not daemon, and I often need to restart it on the command line.

Most of the time it works fine, but sometimes "bind: address in use" is reported, so restart the loss.

Defeat.

A: Andrew Gierth

The server program should always set the SO_REUSEADDR socket option before calling bind (). As for

TIME_WAIT status, which you can't avoid, is part of the TCP protocol.

Q: how to avoid waiting for 60 seconds before restarting the service

A: Erik Max Francis

Use setsockopt, such as

Int option = 1

If (setsockopt (masterSocket, SOL_SOCKET, SO_REUSEADDR, & option)

Sizeof (option))

< 0 ) { die( "setsockopt" ); } -------------------------------------------------------------------------- Q: 编写 TCP/SOCK_STREAM 服务程序时,SO_REUSEADDR到底什么意思? A: 这个套接字选项通知内核,如果端口忙,但TCP状态位于 TIME_WAIT ,可以重用 端口。如果端口忙,而TCP状态位于其他状态,重用端口时依旧得到一个错误信息, 指明"地址已经使用中"。如果你的服务程序停止后想立即重启,而新套接字依旧 使用同一端口,此时 SO_REUSEADDR 选项非常有用。必须意识到,此时任何非期 望数据到达,都可能导致服务程序反应混乱,不过这只是一种可能,事实上很不 可能。 一个套接字由相关五元组构成,协议、本地地址、本地端口、远程地址、远程端 口。SO_REUSEADDR 仅仅表示可以重用本地本地地址、本地端口,整个相关五元组 还是唯一确定的。所以,重启后的服务程序有可能收到非期望数据。必须慎重使 用 SO_REUSEADDR 选项。 Q: 在客户机/服务器编程中(TCP/SOCK_STREAM),如何理解TCP自动机 TIME_WAIT 状 态? A: W. Richard Stevens 下面我来解释一下 TIME_WAIT 状态,这些在 中2.6节解释很清楚了。 MSL(最大分段生存期)指明TCP报文在Internet上最长生存时间,每个具体的TCP实现 都必须选择一个确定的MSL值。RFC 1122建议是2分钟,但BSD传统实现采用了30秒。 TIME_WAIT 状态最大保持时间是2 * MSL,也就是1-4分钟。 IP头部有一个TTL,最大值255。尽管TTL的单位不是秒(根本和时间无关),我们仍需 假设,TTL为255的TCP报文在Internet上生存时间不能超过MSL。 TCP报文在传送过程中可能因为路由故障被迫缓冲延迟、选择非最优路径等等,结果 发送方TCP机制开始超时重传。前一个TCP报文可以称为"漫游TCP重复报文",后一个 TCP报文可以称为"超时重传TCP重复报文",作为面向连接的可靠协议,TCP实现必须 正确处理这种重复报文,因为二者可能最终都到达。 一个通常的TCP连接终止可以用图描述如下: client server FIN M close ----------------->

(passive shutdown)

ACK Mirrle1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report