Connection establishment and disconnection process: CLOSED: indicates the initial state.
LISTEN: this is also an easy-to-understand state, indicating that a SOCKET on the server is in the listening state and can accept connections.
SYN_RCVD: this status indicates that the SYN packet is received. Normally, this status is an intermediate status of the server SOCKET during the three-way handshake session when a TCP connection is established. It is very short, basically, you can hardly see this status with netstat unless you write a client test program and deliberately disable the last ACK packet from the three TCP handshakes. Therefore, when an ACK packet is received from the client, it enters the ESTABLISHED status.
SYN_SENT: this status echo SYN_RCVD. When the client SOCKET executes the CONNECT connection, it first sends the SYN packet, and then it enters the SYN_SENT status, and wait for the server to send 2nd messages in the three-way handshake. SYN_SENT status indicates that the client has sent SYN packets.
ESTABLISHED: This is easy to understand, indicating that the connection has been ESTABLISHED.
FIN_WAIT_1: The FIN_WAIT_1 and FIN_WAIT_2 statuses both indicate waiting for the FIN packets of the other party. The difference between the two States is that the FIN_WAIT_1 state is actually when the SOCKET is in the ESTABLISHED State, it wants to actively close the connection and send a FIN packet to the other party, the SOCKET enters the FIN_WAIT_1 state. When the other Party responds to the ACK packet, it enters the FIN_WAIT_2 status. Of course, under normal circumstances, the other party should immediately respond to the ACK packet, regardless of the situation, therefore, the FIN_WAIT_1 status is generally difficult to see, while the FIN_WAIT_2 status is often seen using netstat.
FIN_WAIT_2: The above has explained in detail this status. In fact, the SOCKET in the FIN_WAIT_2 status indicates a semi-connection, that is, either party requires a close connection, but also tells the other party, I have some data to send to you, and close the connection later.
Time_wait: indicates that the FIN packet of the other party is received, and the ACK packet is sent concurrently, so that 2msl can return to the closed available status. If fin_wait_1 receives a message with both the fin mark and ACK mark, it can directly enter the time_wait status without passing through the fin_wait_2 status.
Closing: this is a special exception that is rare in actual situations. Normally, when you send a FIN packet, you should first (or simultaneously) receive the ACK packet from the other party and then receive the FIN packet from the other party. However, the closing status indicates that after you send the FIN packet, you have not received the ACK packet from the other party, but have also received the FIN packet from the other party. Under what circumstances will this happen? In fact, it is not difficult to come to the conclusion that if both parties close a socket at the same time, both parties may send FIN packets at the same time, that is, the closing status may occur, both parties are closing the socket connection.
Close_wait: the meaning of this state is actually waiting to be closed. How can this problem be solved? When the other party closes a socket and sends a FIN packet to itself, your system will undoubtedly respond to an ACK packet to the other party, and then enters the close_wait status. Next, in fact, what you really need to consider is to check if you still have data to send to the other party. If not, you can close the socket and send the FIN packet to the other party, that is, close the connection. So what you need to do in close_wait is to wait for you to close the connection.
Last_ack: this status is easy to understand. It passively closes a side and waits for the other side's ACK packet after sending the fin message. After receiving the ACK message, you can enter the closed available status. The number of connections is related to the file system handle. You can use ulimt-N to check whether/etc/security/limit can be modified. conf sets the number of nofiles of a user in some highly concurrent systems. A large number of time_wait and close_wait statuses are often displayed. The following describes how to optimize these two statuses. Time_wait: a large number of such statuses often indicate that the system has a high concurrency. A large number of connections are established and released. You can use the persistent connection method or quickly recycle the short connection./etc/sysctl. conf
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30 Then execute
/sbin/sysctl -pMake the parameter take effect.
Net. ipv4.tcp _ syncookies = 1Enable SYN Cookies. When a SYN wait queue overflows, cookies are enabled to prevent a small number of SYN attacks. The default value is 0, indicating that the process is disabled;
Net. ipv4.tcp _ tw_reuse = 1Indicates that reuse is enabled. Allow TIME-WAIT sockets to be re-used for a New TCP connection. The default value is 0, indicating that the TCP connection is disabled;
Net. ipv4.tcp _ tw_recycle = 1Enables fast recovery of TIME-WAIT sockets in TCP connections. The default value is 0, indicating that TIME-WAIT sockets is disabled.
Net. ipv4.tcp _ fin_timeoutChange the system's default TIMEOUT time CLOSE_WAIT: the Server program is in the CLOSE_WAIT status, rather than the LAST_ACK status. It indicates that no FIN has been sent to the Client, this may be because there is still a lot of data to be sent or other things to be done before closing the connection. As a result, the FIN packet is not sent, but the client socket has been disconnected.
Generally, a CLOSE_WAIT will last for at least two hours. If a rogue writes a program specially, it will create a bunch of CLOSE_WAIT for you, consuming
Your resources usually fail to be released, and the system has fixed the crash.
You can only modify the TCP/IP parameters to shorten the time: modifying the tcp_keepalive _ * series parameters helps solve this problem.