I finally understood TCP’s three-way handshake and four waved hands (the picture case is super detailed)

1. Introduction to TCP

1. TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte-stream-based communication protocol . A connection must be established before data is transmitted, and the connection must be disconnected after transmission.
2. The client must use the connect() function to establish a connection with the server before sending and receiving data. The purpose of establishing a connection is to ensure that the IP address, port, and physical link are correct, and to open up a channel for data transmission.
3. Three data packets are transmitted when TCP establishes a connection, commonly known as Three-way Handshaking

Insert picture description here

2. TCP datagram structure

Insert picture description here

①Sequence number : Seq (Sequence Number) occupies 32 bits and is used to identify the sequence number of the data packet sent from computer A to computer B. The computer will mark this when sending data.
②Acknowledgement number : Ack (Acknowledge Number) The confirmation number occupies 32 bits, which can be sent by both the client and the server, Ack = Seq + 1. ③Flag bit : each flag bit occupies 1Bit, there are 6 in total, namely URG, ACK, PSH, RST, SYN, FIN. The specific meanings are as follows:

URG: Urgent pointer is valid.
ACK: Confirm that the serial number is valid.
PSH: The receiver should deliver this message to the application layer as soon as possible.
RST: Reset the connection.
SYN: Establish a new connection.
FIN: Disconnect a connection.

Three. TCP's three-way handshake

Process description

①First, the Client side sends a connection request message

②The Server section replies with an ACK message after accepting the connection, and allocates resources for this connection.

③After receiving the ACK message, the Client also sends an ACK message to the Server segment and allocates resources, so that the TCP connection is established.

Insert picture description here

Use Sichuan Airlines as an example

① Sichuan 8633 requests to establish a connection (SYN) and sends out the serial number.
②The server receives the signal, that is, there is an acknowledgment number (ACK), and also returns the request sequence number Seq at this time.
③The client receives the signal, that is, there is an acknowledgment number (ACK), and the connection has been established.
Insert picture description here
Summary : 三次握手的关键是要确认对方收到了自己的数据包This goal is achieved through the "Ack" field. The computer will record packet sequence number Seq own transmitted until the other party receives the packet, detects "acknowledgment number (the Ack)" field 看Ack = Seq + 1是否成立,如果成立说明对方正确收到了自己的数据包.


If there are only two handshake 这个时候客户端没有回应,这样会浪费服务端的资源

Insert picture description here

Have you thought about why the third communication is needed?

1. In the first communication process, after A sends a message to B, B can confirm that there is no problem with his receiving ability and A's sending ability after receiving the information.

2. In the second communication, after B sends a message to A, A can confirm that there is no problem with his sending ability and B's receiving ability, but B does not know what his sending ability is, so a third is needed. Times communication.

3. In the third communication, after A sends a message to B, B can confirm that there is no problem with his ability to send messages.

4. Summary : The three-way handshake completes two important functions, 既要双方做好发送数据的准备工作(双方都知道彼此已准备好)and both parties must also be allowed to negotiate the initial sequence number, which is sent and confirmed during the handshake process.


Four. Four waves of TCP

Establishing a connection is very important, it is a prerequisite for correct data transmission; disconnecting is equally important, it allows the computer to release resources that are no longer in use. If the connection cannot be disconnected normally, it will not only cause data transmission errors, but also cause the socket to not be closed and continue to occupy resources. If the amount of concurrency is high, the server pressure will be worrying.

//Process description

  • A: "The task is finished, I want to disconnect."
  • B: "Oh, isn't it? Please wait a moment, I'll prepare it."
  • After waiting for a while...
  • B: "I'm ready to disconnect."
  • A: "Ok, thank you for your cooperation."

Insert picture description here

Examples of Sichuan Airlines Chart

  • ① The server applies for disconnection, namely FIN, and sends Seq+Ack
  • ②The client receives the information and returns, indicating that I have received it
  • ③The client sends a message indicating that the connection can be disconnected
  • ④The server accepts the information, and returns data to indicate that the information has been accepted

Insert picture description here

After the data transmission is complete, both parties can release the connection. At the very beginning, both the client and the server are in the ESTABLISHED state, and then the client actively shuts down and the server passively shuts down.


Why is there a three-way handshake when connecting, but a four-way handshake when closing?

  • ①Because when the Server side receives the SYN connection request message from the Client side, it can send a SYN+ACK message directly. The ACK message is used for reply, and the SYN message is used for synchronization.
  • ② However, when the connection is closed, when the server receives a FIN message, it may not immediately close the SOCKET, so it can only reply with an ACK message and tell the client, "I received the FIN message you sent." .
  • ③I can only send FIN messages until all the messages on my Server side have been sent, so I cannot send them together. Therefore, a four-step handshake is required.

Supplementary question

  • Can TCP's three-way handshake guarantee reliable transmission?不能
  • The three-way handshake is more reliable than the two, but it is not completely reliable, and adding more handshake does not make the connection more reliable. Therefore, a three-way handshake was chosen.
  • There is no completely reliable communication protocol in the world. In terms of communication time, space cost, and reliability, the "three-way handshake" is selected as the general rule of point-to-point communication.

Guess you like

Origin blog.csdn.net/m0_50654102/article/details/114811749