There are four state changes in the TCP disconnection process, as shown in:
The following process occurs during connection revocation: 1. the application on host1 closes its connection, causing TCP to send a fin message to host2. 2. host2 sends a confirmation message to host1, and host2 submits fin as EOF to the application on host2. 3. After a period of time, the application on host2 closes the connection over it and triggers a fin message to host1. 4. host1 sends a confirmation message to host2. then, host2 closes the connection and releases the resource. However, host1 does not close the connection, but enters the time_wait status and sets the maximum survival time of the two segments (2msl) this status is retained.
Why?Time_wait
? 1. in step 4, The ack sent by host1 may be lost, causing host2 to resend the fin message, and time_wait maintains the connection status. what will happen if host1, the owner of the active shutdown, does not enter the time_wait status and closes the connection? When the re-transmitted fin message arrives, because TCP no longer has connection information, it uses RST (restart) to respond to the message, resulting in host2 entering the wrong status rather than the ordered termination status, if the last ACK message sent is in the time_wait status and the connection information is still recorded, it can correctly respond to the fin message of the Peer host2. 2. time_wait provides the time for the "outlier segment" in the connection to disappear from the network. consider what will happen if the delay or retransmission segment arrives after the connection is closed? Generally, TCP only discards the data and responds to the RST message, so this will not cause any problems. When the RST message arrives at the host that sends the delay segment, it also discards the segment because the host does not record any connection information. However, if a new connection with the same port number is established between two identical hosts, the outlier segment may be considered as a new connection, if any serial number of the data in the outlier segment is in the current Receiving Window of the new connection, the data will be re-received, and the result is that the new connection is damaged.
In Windows, you can view the status through cmd, netstat-p tcp. Another article, transferred from http://ahuaxuan.iteye.com/blog/657511
A recent socket. Close problem occurs on an application server (execute netstat-NP | grep TCP ):
TCP 0 0 10.224.122.16: 50158 10.224.112.58: 8788 close_wait
TCP 0 0 10.224.122.16: 37655 10.224.112.58: 8788 close_wait
TCP 1 0 127.0.0.1: 32713 127.0.0.1: 8080 close_wait
TCP 38 0 10.224.122.16: 34538 10.224.125.42: 443 close_wait
TCP 38 0 10.224.122.16: 33394 10.224.125.42: 443 close_wait
TCP 1 0 10.224.122.16: 18882 10.224.125.10: 80 close_wait
TCP 1 0 10.224.122.16: 18637 10.224.125.10: 80 close_wait
TCP 1 0 10.224.122.16: 19655 10.224.125.12: 80 close_wait
........................................
There are a total of 200 close_wait sockets, and these sockets cannot be released for a long time. Let's take a look at why this large number of socket close_wait occurs.
First, we need to figure out who initiated the socket. We can see that 122.16 has opened many ports and the port numbers are large, ports on 125.12 or 125.10 are common server ports, so there are so many close_wait ports on 122.16.
The socket is enabled by 122.16. In other words, this machine is a traditional client, and it will actively request the service ports of other machines.
To figure out why close_wait appears, we must first understand the mechanism and principle of close_wait.
Suppose we have a client, a server.
When the client initiates a socket. Close (), what will happen to TCP? As shown in.
Bytes
The client first sends a fin signal to the server. At this time, the client becomes the fin_wait_1 state. After the server receives the fin, it returns ack and the server status changes to close_wait.
Then the server needs to send a fin to the client, and the server status changes to last_ack. Then the client returns an ACK, and the server socket is successfully closed.
From this point, we can see that if the client closes a link, the client will not be in the close_wait status. If the client closes the link, the server will be in the close_wait status.
On our server, close_wait occurs in the client socket. Therefore, this is because the server proactively closes the socket on the server.
So when the server initiates a socket. Close (), what happened at this time.
Bytes
We can see that if the server closes the link, the client may enter close_wait. If the client does not send the fin package, then the client will remain in the close_wait state (we can see that there are parameters to adjust this time ).
Now we need to figure out why the client does not send the FIN packet to the server in the second scenario. to clarify this problem, we must first understand how the server sends the fin package to the client. In fact, the server calls
Socket. the close method is used only. That is to say, if the client wants to send the fin package, the client must call the socket. close, otherwise the client will always be in close_wait (but in fact the implementations of different operating systems are different,
This case also appears in the ahuaxuan (ahuaxuan.iteye.com) example ).
Http://www.cnblogs.com/kevin-wu/archive/2006/11/27/574369.html this can also refer