TCP three handshake and four waves the most concise and understandable explanation __tcp

Source: Internet
Author: User
Tags ack

It takes three handshake to establish TCP and four handshake to disconnect. The entire process is shown in the following illustration:


The process of establishing a connection:
A TCP connection is initialized with a three-time handshake. The purpose of the three handshake is to synchronize the serial number and confirmation number of both sides of the connection and to exchange the TCP window size information. The following steps outline the process that typically occurs when a client computer contacts a server computer:
1. The client sends a SYN-bit TCP message to the server that contains the initial serial number x of the connection and a window size (representing the size of the buffer on the client to store incoming segments sent from the server).
2. When the server receives the SYN message sent by the client, it sends a SYN and ACK-bit TCP message to the client, which contains the initial serial number y of its choice and the confirmation of the serial number of the client x+ 1 and a window size (representing the size of the buffer on the server that is used to store incoming segments sent from the client).
3. When the client receives the Syn+ack message returned by the server, it returns a confirmation number y+1 and a serial x+1 ACK message to the server side, and a standard TCP connection completes.



The process of disconnecting:


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 state experienced by the client side of the process:



The process experienced by the server side:


"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 side receives the 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 in principle, 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.

Question 3 Why you cannot connect with a two-time handshake.
A: 3 times to shake hands to complete two important functions, both sides do a good job of sending data preparation (both sides know each other is ready), but also to allow both sides to negotiate the initial serial number, this serial number in the handshake is sent and confirmed.
       Now change the three handshake to just two handshakes, and deadlocks can occur. As an example, consider the communication between the computer S and C, assuming C sends a connection request group to S, and S receives the packet and sends a confirmation response group. According to the two handshake agreement, S believes that the connection has been successfully established and can begin sending data groupings. However, if C is lost in the transmission of the answer packet of S, it will not know if S is ready, do not know what serial number the s builds up, and C even doubts whether S has received its own connection request grouping. In this case, C considers that the connection has not yet been successful and will ignore any data packets sent by S, waiting only for the connection to confirm the response group. and S sends the same grouping again after the group that it has timed out. This creates a deadlock.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.