Three handshakes and four waves of the TCP protocol

Source: Internet
Author: User

The information you need for the moment is:

The ACK:TCP agreement stipulates that only ack=1 is valid and that all messages sent after the connection is established must have an ACK of 1

SYN (synchronization): Used to synchronize the sequence number when the connection is established. When Syn=1 and ack=0, it indicates that this is a connection request message. If the other party agrees to establish the connection, the Syn=1 and ack=1 should be made in the response message. Therefore, the SYN 1 indicates that this is a connection request or a connection acceptance message.

FIN (Finis) is the end, which means to release a connection. When FIN = 1 o'clock, it indicates that the sender's data for this segment has been sent, and that the connection is required to be released.

Establishing TCP requires three handshakes to be established, while disconnecting requires four handshakes. The entire process is as follows:

Let's take a look at how to establish a connection:

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

First, the client makes a request to connect the Syn=1 ack=0 (see the description of the field), TCP rules syn=1 can not carry data, but to consume a sequence number, so declare their ordinal is seq=x

The Server then responds with confirmation that syn=1 ack=1 seq=y, ack=x+1,

Then the Client again to confirm, but no SYN, this is ack=1, seq=x+1, ack=y+1.

Then the connection is established, why do we have three handshake (two confirmations).

How do I disconnect it? The simple process is as follows:

"Note" The interrupt connection can be either client side or server side.

Suppose the client side initiates an interrupt connection request, which is to send a fin message. After the server receives the fin message, it means " my client has no data to send to you ", but if you have data that is not sent, you do not need to close the socket, you can continue to send data. So you first send an ACK, " tell the client that your request I received, but I am not ready, please continue to wait for my message ." At this point the client enters the fin_wait state and continues to wait for Fin messages on the server side. 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, my side of the data is finished, ready to close the connection ." Client side received fin message, " I know can shut down the connection, but he still do not believe the network, afraid 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 re-transmitted . "When the server side receives an ACK," You know you can disconnect . " Client side waiting for 2MSL still not received a reply, the server side has been properly shut down, well, I can also close the client terminal connection . The OK,TCP connection is closed like this!

The client side of the process is experiencing the following status:

"Note" in the time_wait state, if the last ACK sent by the TCP client is lost, it will be re-sent. 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 waiting, and all resources (including the port number) are released.

"Question 1" Why is the handshake three times when it is connected and four times when it is closed?
A: The syn+ack message can be sent directly after the server receives the SYN connection request message from the client side. Where the ACK message is used to answer, the SYN message is used for synchronization. However, when the connection is closed, when the server side receives the fin message, it is likely that the socket will not be closed immediately, so you can only reply to an ACK message, tell the client side, "You send fin message I received." I can't send fin messages until all the messages on my server end are sent, so I can't send them together. Therefore, four-step handshake is required.

"Question 2" Why does the time_wait state need to go through 2MSL (maximum message segment lifetime) to return to the close state?

A: Although according to reason, four messages are sent, we can go directly to the close state, but we must pretend that the network is unreliable, there can be a last ACK lost. So the TIME_WAIT state is used to re-send the possible missing ACK messages.

Three handshakes and four waves of the TCP protocol

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.