Three-way handshake and four-way wave in TCP protocol (illustration)

Establishing a TCP requires three handshakes to establish, and disconnecting requires four handshakes. The whole process is shown in the figure below:

Let's take a look at how to establish a connection first.


First, the client side sends a connection request message. After the server segment accepts the connection, it replies with an ACK message and allocates resources for this connection. After receiving the ACK message, the client also sends an ACK message to the Server segment and allocates resources, so that the TCP connection is established.

So how to disconnect? The simple process is as follows:

[Note] The interrupted connection end can be the client end or the server end.

Suppose the client side initiates a disconnection request, that is, sends a FIN message. After the server receives the FIN message, it means " my client has no data to send to you ", but if you still have data that has not been sent, you don't have to rush to close the Socket, you can continue to send data. So you send ACK first, " tell the client that I have received your request, but I am not ready, please continue to wait for my message ". At this time, the client side enters the FIN_WAIT state and continues to wait for the FIN message from the server side. When the server side determines that the data has been sent, it sends a FIN message to the client side, " tell the client side, OK, I have finished sending the data here, and I'm ready to close the connection ". After the client receives the FIN message, " it knows that the connection can be closed, but he still does not believe in the network, for fear that the server does not know to close, so it enters the TIME_WAIT state after sending ACK. If the server does not receive an ACK, it can restart. " Pass . ", after the server receives the ACK, " it knows that the connection can be disconnected ". After the client side waits for 2MSL and still does not receive a reply, it proves that the server side has been shut down normally. Well, my client side can also close the connection . Ok, the TCP connection is closed like this!

The state experienced by the client in the whole process is as follows:

 


The process experienced by the server side is as follows: please indicate when reprinting: blog.csdn.net/whuslei

[Note]  In the TIME_WAIT state, if the last ACK sent by the TCP client is lost, it will be resent . The time required in the TIME_WAIT state is implementation-dependent. Typical values ​​are 30 seconds, 1 minute and 2 minutes. After waiting, the connection is officially closed and all resources (including port numbers) are released.

[Question 1] Why is there a three-way handshake when connecting, but a four-way handshake when closing?
Answer: Because when the server receives the SYN connection request message from the client, it can directly send the SYN+ACK message. The ACK message is used for response, and the SYN message is used for synchronization. However, when closing the connection, when the server receives the FIN message, it is likely not to close the SOCKET immediately, so it can only reply an ACK message to tell the client, "I received the FIN message you sent". I can only send FIN messages until all the messages on my server have been sent, so I cannot send them together. Therefore, a four-step handshake is required.

[Question 2] Why does the TIME_WAIT state need to pass 2MSL (maximum segment lifetime) before returning to the CLOSE state?

Answer: Although it is reasonable to say that all four packets have been sent, we can directly enter the CLOSE state, but we must pretend that the network is unreliable, and the last ACK may be lost. So the TIME_WAIT state is used to resend ACK messages that may be lost.

Reprinted from Great God: http://blog.csdn.net/whuslei/article/details/6667471/

Supplement: Regarding the TCP protocol, I think you should understand it!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685665&siteId=291194637