TCP shakes hands three times and waves four times

Time:2021-8-25

TCP triple handshake

TCP triple handshake process

Suppose there is a sender computer and a receiver computer, and the vertical axis is the time axis

TCP shakes hands three times and waves four times

First handshake

Suppose that the sender actively establishes a connection with the receiver, so the sender will send a message for the first time (syn = 1, indicating that this is a message of connection request, and seq = x is the serial number of the synchronous sender)

The Second Handshake

After receiving the connection request, the receiver will open the TCP connection. At the same time, it will also send a message, which is the second handshake. Message information includes:

  • SYN=1: indicates a connection request
  • ACK=1: indicates confirmation of serial number
  • ack=x+1: ack in lowercase indicates the confirmation number. ACK = x + 1 here means that the receiver expects to receive the value of the serial number x + 1
  • seq=y: at the same time, the message sent by the receiver will also carry its own serial number, that is, SEQ = y
Third handshake

After receiving the message, the sender will respond. The message content in the response is as follows:

  • ACK=1: indicates that the confirmation number of this message is valid
  • seq=x+1: the serial number carried by the sender, which means that the data serial number sent by the current sender is x + 1
  • ack=y+1: the confirmation number is y + 1, indicating that the sender expects to receive the data with the serial number of Y + 1 from the receiver

Through these three handshakes, the TCP connection is established

Key information in three handshakes

  • Both the first and second handshakesSyn tag, indicating that this is a connection request
  • Both the second and third handshakesACK tag, for ACK, it actually synchronizes the serial numbers of both sides of the connection. For example, through two ack synchronizations, the sender already knows what the receiver’s ack is. At the same time, the receiver also knows what the sender’s ack is. Through three handshakes, they not only establish the connection, but also synchronize their respective serial numbers

In the time axis of the three handshakes, the receiver and the sender have different states at different times

  • Until the receiver receives no data, it is inmonitorStatus (listen)
  • Between sending the first message and receiving the response of the first message, the sender belongs toSync sentStatus (sync-sent), indicating that the syn has been sent and waiting for the other party’s syn information
  • From the first message sent by the receiver to the second message received, it belongs toSync received status(sync-rcvd) indicates that I have received the syn message sent to me by the sender
  • Then the sender entersEstablish connection(established) status
  • For the sender, as long as the second handshake is successful, the sender will establish a connection. But for the receiver, it is only after receiving the sender’s third handshakeStatus of establishing connection(ESTABLISHED)

The time for the two sides to establish the connection state is different. As long as the sender succeeds in the second handshake, it becomes the established connection state. However, for the receiver, the connection is established only after receiving the third handshake from the sender. After both parties enter the state of establishing a connection, data can be transmitted

Why does the sender send a third acknowledgement message? Why not twice?

Conclusion: avoid sending the failed connection request message to the other party and causing errors

Suppose there is a sender computer and a receiver computer. Firstly, the sender needs to send a connection establishment request message (first handshake). Suppose that the message of the first handshake is transmitted in the network for a long time before reaching the receiver. Because it has been sent for a long time, so,The sender has not received the confirmation message from the receiver for a long time。 The sender will think that the first message has timed out, so the sender will send the same message a second time

TCP shakes hands three times and waves four times

Assuming that the message sent for the second time soon reaches the other party, the receiver will respond after receiving the second connection request message and establish the connection between them. Then, the first request message sent by the sender should be aInvalid request message, because its function has been completed by the second connection request. Therefore, the connection request message sent for the first time, which has been wandering in the network for a long time, is actually an invalid request message and has no effect

TCP shakes hands three times and waves four times

What happens if both connection requests sent by the sender establish a connection?

First, consider the message of the second request. The message arrives at the receiver in advance. The receiver will respond to it. After the response is confirmed, the connection is established(Because we assume that the connection is established after two handshakes

Now consider the connection request sent for the first time,If the connection is established after two handshakes, for a failed request, it will also establish a connection, because as long as the receiver responds, it means that the connection has been established

This will lead to the situation that two TCP connections will be established when the same request is sent twice. This situation is wrong, so two handshakes are incorrect

How do three handshakes solve the problems caused by two handshakes?

For two handshakes, as long as the receiver responds, the connection is established. andFor three handshakes, the first confirmation message will reach the sender first, and then the sender will send another confirmation message (the third handshake). At this time, the connection is established

TCP shakes hands three times and waves four times

Now consider the connection request message that arrives at the receiver slowly. For this message, the receiver will also send an acknowledgement message to the sender (the second handshake). However, the sender has shaken hands for the third time, so the sender will respond to the second confirmation messageIgnore, no operation will be performed. In this way, the first slow connection request will not establish a connection, which avoids the error caused by two handshakes

TCP’s four waves

TCP four wave process

Or suppose there is a sender’s settlement machine and a receiver’s computer, and the vertical axis is the time axis. When the connection is normal, both sides can always transmit data. Assuming that the data transmission is completed, the TCP connection will be released. Suppose the sender actively releases the connection

TCP shakes hands three times and waves four times

First wave

The sender sends the message of waving for the first time, and the message content is as follows:

FIN=1: this flag indicates that the connection needs to be released

seq=u: synchronize your serial number to the receiver

At this point, the sender entersThe first wait state at the end of the connection(FIN-WAIT-1)

Second wave

After receiving the disconnection request from the sender, the receiver will also send a message to confirm that I have received the serial number sent by the other party. The content of the confirmation message is:

ACK=1: indicates that this message has been confirmed

seq=v: synchronize its own serial number

ack=u+1: the confirmation number is U + 1, which indicates that the receiving party expects to receive the data with the serial number of U + 1 from the receiving and transmitting party

After receiving the confirmation message, the sender enters theThe second wait state at the end of the connection(FIN-WAIT-2)。 After sending the first confirmation message, the receiver enters theTurn off wait state(CLOSE-WAIT)

At this time, the receiver can still send data, because the request to release the connection is initiated by the sender, which means that the sender’s data transmission has been completed, but the receiver may not have completed the transmission

Third wave

After sending the first confirmation message, the receiver will send a new message. This message will carry the mark fin = 1, indicating that it can also be connected and released, and an ACK will be carried inside, indicating that the serial number sent by the sender is repeatedly confirmed. The complete contents of this message are as follows:

FIN=1: this flag indicates that the connection needs to be released. It is a request to release the connection

ACK=1: indicates that the confirmation message has been received

seq=w: synchronize your serial number to the sender

ack=u+1: the confirmation number is U + 1, which indicates that the receiving party expects to receive the data with the serial number of U + 1 from the receiving and transmitting party

Fourth wave

After receiving the confirmation message from the receiver, the sender will send another confirmation message to confirm that I have received the message sent by the receiver. At this time, the connection can be released

When the receiver sends the disconnection request to the sender and the acknowledgement message is received by the receiver, the receiver is inFinal confirmation status(LAST-ACK)。 This is to confirm that the sender has received the connection release message. At this time, the sender enters theWait timer status(TIME-WAIT)。 The sender will wait for a period of time in this time waiting state to ensure that there are no problems in this period of time before enteringOff state(CLOSE)

The above is the process of four waves

TCP shakes hands three times and waves four times