Establishing TCP requires a three-way handshake to establish, while disconnecting requires a four-way handshake. The whole process is shown in the figure below:
Let’s first look at how to establish a connection.
【Updated on 2017.01.04】The picture in this part is wrong, please forgive me, the correct picture is as follows, the error The accompanying pictures will not be deleted. You can compare them for better understanding. Sorry for taking so long to update! !
##The wrong picture is as follows:
First, the Client 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. How to disconnect? The simple process is as follows:[Note] The interrupt connection end can be the Client end or the Server end.
Assume that the Client initiates a connection interruption 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 and you can continue. send data. So you send ACK first, "Tell the client that I have received your request, but I am not ready yet, please continue waiting for my message". At this time, the Client enters the FIN_WAIT state and continues to wait for the FIN message from the Server. When the server determines that the data has been sent, it sends a FIN message to the client, " Tell the client, OK, I have finished sending the data and I am ready to close the connection". After the client receives the FIN message, " knows that the connection can be closed, but it still does not trust the network and is afraid that the server does not know to close it, so it enters the TIME_WAIT state after sending the ACK. If the server does not receive the ACK Then you can retransmit . "After the server receives the ACK, " will know that the connection can be disconnected". The client still has not received a reply after waiting for 2MSL, which proves that the server has been closed normally. Well, the client can also close the connection. Ok, the TCP connection is closed like this! The status experienced by the Client during the entire process is as follows:
The process experienced by the Server is as follows:
[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. But when closing the connection, when the server receives the FIN message, it is likely that the SOCKET will not be closed immediately, so it can only reply with an ACK message first, telling the client, "I received the FIN message you sent." Only when all the messages on my server side have been sent can I send the FIN message, so it cannot be sent together. Therefore, a four-step handshake is required.
[Question 2] Why does the TIME_WAIT state need to pass 2MSL (maximum segment survival time) before it can return to the CLOSE state?
Answer: Although it stands to reason that all four messages have been sent and we can directly enter the CLOSE state, we must pretend that the network is unreliable and the last ACK may be lost. Therefore, the TIME_WAIT state is used to resend ACK messages that may be lost.
The above is the detailed content of What do the three-way handshake and the four-way handshake mean in TCP. For more information, please follow other related articles on the PHP Chinese website!