The introduction

The TCP three-way handshake and four-way wave are common interview questions and very important parts of the TCP protocol. Since TCP is a connection-oriented protocol, the designers must have carefully planned how to gracefully establish and release connections.

Preliminary knowledge

TCP header

We need to focus on the order number, confirmation number, and ACK, SYH, and FIN flag bits. The serial number and confirmation number not only play a role in TCP connection and disconnection, but also play a very important role in the reliable transmission of TCP. Connection and disconnect messages are essentially packets, and the reliability of transmission between them is highly dependent on the TCP acknowledgement mechanism.

Confirm the mechanism

In TCP, when the sender sends data, it determines a serial number for the data. As the number of sent data increases, the serial number gradually increases until the number exceeds 65535, and then the number starts from 0. After receiving the data, the receiving end sends an acknowledgement signal to the receiving end, which is written in the acknowledgement number. The meaning of the acknowledgement signal is as follows: Assuming that the acknowledgement number is X +1, the sending end has received the packet with the serial number x+1 and expects to receive the packet with the serial number X +1.

Connection established (three handshakes)

  1. The requester sends a synchronization packet to the receiver. The synchronization packet contains no content but the TCP header. As the first message of this connection (although the connection may not succeed), its serial number is not 0, but0655 [35]A random number between, let’s sayx. And in the headerSYNThe flag bit is 1.
  2. The receiver has been listening to a series of port machine, when receiving side received the request and send the first request connection message, if you allow it connection, will be sent a confirmation message, the packet is still not the data content, the header also have specific requirements: to confirm, just received request connection signal so confirmation number isx+1, because the sequence number of the packet to be confirmed isxAnd at the same timeACKFlag bit is 1; At the same time, as a message, it also has its own serial number, also a0655 [35]A random integer of PI, let’s sayy; It’s not just a message, it’s a message that establishes a connection, soSYNAlso to 1
  3. After receiving the synchronization packet, the sender agrees with the connection. Then, the sender sends the last acknowledgement packet to confirm the synchronization packet sent by the receiver:ACKThe flag bit is 1, and the confirmation sequence number isy+1And the serial number changes tox+1Because the sequence number of the last message sent isx.

Connection release (four waves)

  1. After sending data, the requester sends an end – connection message to the receiver: in the headerFINThe flag bit is 1 and the serial number isx, because the data has been sent before, soxIs the next value of the sequence number of the previously sent message.
  2. The receiver receives a connection release signal and sends an acknowledgement of the message. The rules for flag bits and serial numbers are similar to the three-way handshake. However, although the sending end has finished sending data, the receiving end is not sure, so the channel is not completely closed, after sending the confirmation signal, the receiving end can still send data to the sending end.
  3. When the data on the receiving end is also sent, the receiving end also sends a signal to the sender to release the connection.
  4. The sender acknowledges the received release signal and releases the connection.

conclusion

There is much more to TCP connection and release than that, and there is a lot of detail involved.