It takes three handshake to establish TCP and four handshake to disconnect. The entire process is shown in the following illustration:
Let's take a look at how to establish a connection.
First, the client side sends the connection request message, the server segment accepts the connection and replies the ACK message, and allocates resources for this connection. The client side receives the ACK message and also takes an ACK message to the server segment and allocates resources so that the TCP connection is established.
How to disconnect it. The simple process is as follows:
Note the interrupt connector can be either a client side or a server side.
Assume that the client side initiates an interrupt connection request, which is to send a fin message. When the server receives the fin message, it means "I have no data to send to you on the client side", but if you still have the data not sent to complete, you do not have to close the socket, you can continue to send data. So you first send an ACK, "Tell the client side, your request I received, but I am not ready, please continue to wait for my message." This time the client side enters the fin_wait state and continues to wait for the server-side fin message. When the server side determines that the data has been sent, the fin message is sent to the client side, "Tell the client side, OK, I'm done with the data, ready to close the connection." The client side received the fin message, "know that you can close the connection, but he still do not believe that the network, afraid of the server side do not know to shut down, so send an ACK into the time_wait state, if the server does not receive an ACK can be retransmission." , "You know you can disconnect" when the server side receives an ACK. The client side waits for 2MSL and still does not receive a reply, it proves that the server side has been shut down properly, OK, I can also shut down the client side of the connection. The OK,TCP connection is closed like this.
The status of the client side of the process is as follows:
The server side is experiencing the following process: Reprint please specify: Blog.csdn.net/whuslei
"Note" in the time_wait state, if the last ACK sent by the TCP client is lost, it will be sent again. The time required in the TIME_WAIT state is dependent on the implementation method. Typical values are 30 seconds, 1 minutes, and 2 minutes. The connection is formally closed after the wait, and all resources, including the port number, are freed.
"Question 1" Why the connection is three times handshake, when the closure is four times handshake.
A: Because when the server receives a client-side SYN connection request message, it can send the syn+ack message directly. Where the ACK message is used for answering, the SYN message is used for synchronization. But when you close the connection, when the server side receives the fin message, it is likely not to immediately shut down the socket, so you can only reply to an ACK message, tell the client side, "You sent the FIN message I received." I can't send a fin message until all the messages in my server end are sent, so I can't send it together. So it takes four steps to shake hands.
Question 2 Why the time_wait state needs to go through 2MSL (maximum segment lifetime) to return to the close state.
A: Although according to the truth, four messages are sent out, we can directly into the close state, but we must pretend that the network is unreliable, there can be the last ACK lost. So the TIME_WAIT state is used to re-send an ACK message that may be lost.