Active closure and passive closure can be used to disable a socket. The former refers to the active shutdown initiated by the local host, while the latter refers to the response made by the local host after detecting that the remote host is closed, thus closing the entire connection.
Shows the status chart:
At first, each socket is in the closed state. When the client initiates a connection, it sends a SYN packet to the server and the client enters the syn_sent state.
The server receives the SYN Packet and returns a SYN-ACK packet. After receiving the packet, the client returns an ACK packet. The client changes to the established State. If the SYN-ACK packet is not received for a long time, the client times out and enters the closed state.
When the server binds and listens to a port, the socket status is listen. When the client attempts to establish a connection, the server receives a SYN Packet and returns the SYN-ACK packet. The server status changes to syn_rcvd. When the client sends an ACK packet, the server socket status changes to established.
When a program is in the established State, there are two ways to close it: active and passive. If you want to disable it, send a fin package. When your program closesocket or shutdown (Mark), your program sends a FIN packet to the peer, and your socket changes to the fin_wait_1 state. Peer reports an ACK packet and your socket enters the fin_wait_2 state. If the peer is also shutting down the connection, it will send a fin package to your computer. You feed back an ACK package and convert it to the time_wait status.
Time_wait status and 2msl wait status. MSL indicates the maximum segment Life Cycle (maximum segment life time) indicates the time when a packet exists between the network and when it is discarded. Each IP packet has a TTL (time_to_live). When it is reduced to 0, the packet is discarded. Each vro reduces the TTL value by one and transmits the packet. When a program enters the time_wait state, it has two MSL times. This allows TCP to resend the last ack. In case the last Ack is lost, the fin is re-transmitted. After 2msl waits for completion, the socket enters the closed state.
Passive close: when the program receives a FIN packet from the peer and returns an ACK packet, the socket of the program is transferred to the close_wait status. Because the peer has been disabled, no messages can be sent. But the program can. To close the connection, the program has been sent to its own fin to change the TCP socket state of the program to the last_ack state. When the program receives the ACK packet from the peer, the program enters the closed state.
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////////////////////////
Run lsof-N httpd | grep TCP with root permission to check whether a large number of close_wait instances exist.
Abstract: This article explains why the socket connection is locked in the close_wait status and how to avoid this situation.
Not long ago, my Socket Client encountered a very embarrassing error. It should have been sending data continuously to the server on a persistent socket connection. If the socket connection is disconnected, the program will automatically retry the connection.
One day, I found that the program was constantly trying to establish a connection, but it always failed. Using netstat to check whether there are thousands of socket connections in the close_wait State, so that the upper limit is reached, so a new socket connection cannot be established.
Why?
Why are they all in the close_wait status?
Reasons for close_wait status generation
First, we know that if our client program is in the close_wait status, the socket is passively closed!
If the server actively breaks the current connection, both parties need four packages to close the TCP connection:
Server ---> fin ---> Client
Server <--- ack <--- Client
At this time, the server is in the fin_wait_2 state, and our program is in the close_wait state.
Server <--- fin <--- Client
When the client sends a fin to the server, the client is set to the last_ack state.
Server ---> ack ---> Client
The server responds to ACK, so the client socket is truly set to closed.
Our program is in the close_wait status, rather than the last_ack status. It indicates that no fin has been sent to the server, so there may be a lot of data to be sent or other things to do before closing the connection, as a result, the FIN packet is not sent.
The reason is clear. Why don't I release a fin package? Is there so much to do before I close my connection?
For example, elssann said that when the other party calls closesocket, my program is calling Recv. At this time, I may not receive the fin package sent by the other party, instead, an ACK packet is returned by TCP, so the socket on my side enters the close_wait status.
Therefore, it is recommended to judge whether the returned value of the Recv function has an error. If yes, it will take the initiative to closesocket to prevent the fin package from being received.
Because we have set the Recv timeout time to 30 seconds, if it is time-out, the error here should be wsaetimedout. In this case, you can also take the initiative to close the connection.
Another question is, Why are thousands of connections in this status? During that time, did the Server Always take the initiative to remove our connections?
In any case, we must prevent similar situations from happening again!
First, we need to ensure that the original port can be reused. This can be done by setting the so_reuseaddr socket option:
Reuse local addresses and ports
In the past, I used to change to another port because thousands of Ports entered the close_wait status. If this happens again next time, I want to add a limit, but the current port is in the close_wait status!
Before calling
Sockconnected = socket (af_inet, sock_stream, 0 );
Then, we need to set the socket options for reuse:
/// Allow reuse of local addresses and ports:
/// The advantage is that, even if the socket is disconnected, calling the preceding socket function will not occupy another function, but always be a port.
/// In this way, the socket cannot be connected at all times, so the original practice will continuously change the port.
Int nreuseaddr = 1;
Setsockopt (sockconnected,
Sol_socket,
So_reuseaddr,
(Const char *) & nreuseaddr,
Sizeof (INT ));
In the textbook, if the server is closed or exited, causing both the local address and port to be In the time_wait status, so_reuseaddr is very useful.
We may not be able to avoid freezing in the close_wait state, but at least we can ensure that it will not occupy the new port.
Next, we need to set so_linger socket options:
Close it easily or forcibly?
Linger means "delay.
By default (Win2k), so_dontlinger socket options are 1; so_linger options are {l_onoff: 0, l_linger: 0 }.
If closesocket () is called while sending data (sending () is not completed, and data is not sent), we usually take the following measures ":
Because I will call
/// Disable two-way communication first
Shutdown (sockconnected, sd_both );
/// For security, close the old connection before each socket connection is established.
Closesocket (sockconnected );
We will do this time:
If so_linger is set to zero (that is, the l_onoff field in the linger structure is set to non-zero, but l_linger is 0), you do not have to worry about closesocket calls going to the "locked" status (waiting for completion ), no matter whether there is queue data not sent or not confirmed. This method is called "force close" because the virtual circuit of the socket is reset immediately, and all data that has not been sent will be lost. All remote Recv () calls fail and the wsaeconnreset error is returned.
Set this option after connect successfully establishes a connection:
Linger m_slinger;
M_slinger.l_onoff = 1; // (allowed to stay when closesocket () is called, but there is still data not sent)
M_slinger.l_linger = 0; // (the allowable stay time is 0 s)
Setsockopt (sockconnected,
Sol_socket,
So_linger,
(Const char *) & m_slinger,
Sizeof (linger ));
Summary
We may not be able to avoid the recurrence of close_wait status freezing, but we will minimize the impact. We hope that the reuse socket option will enable close_wait to be kicked off during the next connection establishment.