Some understanding about TCP three-way handshake

TCP three-way handshake to establish communication notes

Full text preparation:

  • TCP: Transmission Control Protocol, a connection-oriented protocol that provides a reliable full-duplex byte stream for user programs.
  • Section: Section is the protocol data unit of the TCP transport layer, generally the smallest unit sent by TCP
  • Data sequence number: TCP sorts the sent data by associating a sequence number to each byte to ensure normal recovery after data reception
  • SYN: SYN is a subsection, does not carry data, only TCP header, IP header and TCP options

The following is a flow chart of TCP three-way handshake intercepted from "UNIX Network Programming Volume 1":

TCP three-way handshake process

  1. The server must first be ready to receive the connection request from the client (initialization is done through the socket, bind, and listen functions, and then blocked by accept to wait for the client's request to arrive) - this is called passive opening
  2. The client calls the connect function to initiate a connection request to the server—called active open; in this step, the client sends a SYN section to the server, telling the server the initial serial number J of the data the client will send in the connection. This completes the first handshake!
  3. After the server receives the client's SYN section, it must send back an acknowledgement (ACK); at the same time, it must also send a SYN section (not just the initial sequence number of the server to the client's data, the client must also know the server's initial sequence of data Only the two-way communication can be performed normally); generally, the server sends SYN and ACK in a single section instead of splitting it into two sections. This is the second handshake.
  4. Since the server receives an acknowledgment (ACK) for the SYN segment from the client, the client should also acknowledge (ACK) after receiving the SYN segment from the server. So the ACK sent back by the client after receiving the SYN from the server is the third handshake.

The following is my understanding of the TCP three-way handshake:

  1. Since TCP is claimed to be reliable, the most basic thing is to ensure the normal sending and receiving functions of both parties, and both parties know that the sending and receiving functions of both parties are normal (that is, I not only need to know that I can send and receive normally, I also need to know that the other party can send and receive normally. ; And strictly speaking, the normal sending and receiving here should include or more refer to the normal communication channel between the client and the server)
  2. So how do you know that the sending and receiving between yourself and the other party is normal?
  3. In the first handshake, the server can normally receive the client's SYN section, indicating that the server's receiving function and the client's sending function are normal
  4. The second handshake client confirms that its receiving and sending functions are normal (think about it, the success of the second handshake is based on the success of the first handshake, that is, the client sends normally), and the server's The receiving and sending functions are normal (similarly, if the server cannot receive (first handshake) or cannot send (second handshake), the second handshake cannot be completed)
  5. The client already knows that the two parties are sending and receiving normally, but the server still cannot fully know whether the two parties are sending and receiving normally. At this time, a third handshake is required
  6. After the client receives the ACK+SYN from the server and sends back the ACK, it can connect and return; and the server needs to receive the ACK from the client before returning. In other words, the client returns after initiating the third handshake, and the server must complete the third handshake before ending the return. Compared with the client, there is a certain delay.

Explain this sentence "Strictly speaking, the normal transmission and reception here should include or more refer to the normal communication channel between the client and the server." TCP is generally not used for communication in a single computer, and the data sent from one end has not yet arrived. The communication channel in front of the other end is called the channel, and the channel is relatively unstable, which will lead to a greater possibility of data loss in the channel; so the normal transmission and reception includes the normal communication of the channel before the reception after the transmission.


reference:

  1. "UNIX Network Programming Volume 1: Socket Networking API" Chapter 2

Guess you like

Origin blog.csdn.net/weixin_41629848/article/details/98648910