Understand the structure of TCP datagrams, three-way handshake, data transmission process, packet loss and solutions, wave four times in one article

table of Contents

1. TCP datagram structure

2. Three handshake

3. The transmission process of TCP data

4. Data packet loss and solutions during transmission

4.1. Packet loss

4.2. Retransmission Time Out (RTO, Retransmission Time Out)

4.3, the number of retransmissions

5. Wave four times (TCP disconnected)


TCP (Transmission Control Protocol, 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.

The client uses the connect() function to establish a connection with the server before sending and receiving data. The purpose of establishing a connection is to protect the correctness of IP addresses, ports, and physical links, and to open up channels for data transmission.

Three data packets are transmitted when TCP establishes a connection, commonly known as Three-way Handshaking. It can be very vivid, such as the following dialogue:

  • [Shake 1] Socket A: "Hello, socket B, I have data to send to you here, let's establish a connection."
  • [Shake 2] Socket B: "Okay, my side is ready."
  • [Shake 3] Socket A: "Thank you for accepting my request.

1. TCP datagram structure

The shaded fields need to be emphasized:

1) Sequence number: Seq (Sequence Number) occupies 32 bits, used to identify the sequence number of the data packet sent from computer A to computer B, and the computer will mark this when sending data

2) Confirmation number: Ack (Acknowledge Number) The confirmation number occupies 32 bits, which can be sent by both the client and the server, Ack = Seq + 1.

3) Flag bit: each pointer occupies a bit, there are 6 bits in total, namely URG, ACK, PSH, RST, SYN, FIN, the specific meaning is as follows

  •     URG: Ungent pointer is valid
  •     ACK: Confirm that the serial number is valid (Acknowledge, confirmation).
  •     PSH: The receiver should deliver this message to the application layer as soon as possible.
  •     RST: Reset the connection.
  •     SYN: Establish a new connection (establish a new synchronization connection).
  •     FIN: Disconnect a connection (Finish, means complete)

2. Three handshake

After the client calls the socket() function to create the socket, the socket is in the CLOSED state because there is no connection established; after the server calls the listen() function, the socket enters the LISTEN state and starts to listen for client requests.

At this time, the client starts to initiate a request:

1) When the client calls the connect() function, the TCP protocol will form a data packet on the client and set the SYN flag, indicating that the data packet is used to establish a new synchronous connection, and a random number 1000 is generated at the same time. Fill in the "Seq" field to indicate the sequence number of the data packet. After completing these tasks and starting to send data packets to the server, the client enters the SYN-SEND state.

2) The server receives the data packet and detects that the SYN flag bit has been set, and knows that this is a "request packet" sent by the client to establish a connection. The server will also form a data packet and set the SYN and ACK flag bits. SYN indicates that the data packet is used to establish a connection, and ACK is used to confirm the data packet sent by the client just now.

The server generates a random number 2000 and fills in the "Seq" field. 2000 has nothing to do with client data packets.

The server adds 1 to the "SYN bit" data of the client packet to get 1001, and fills the "Ack" field with this number.

The server sends out the data packet and enters the SYN-RECV state.

3) The client receives the data packet and detects that the SYN and ACK flags have been set, and knows that this is an "acknowledgement packet" sent by the client. The client will check the "Acknowledgement Number (Ack)" field to see if its value is 1000+1. If it is, it means that the requesting party to create a new connection has successfully received it.

Next, the server will continue to construct the data packet and set the ACK flag, indicating that the client correctly received the "confirmation packet" from the server. At the same time, add 1 to the data packet (2000) sent by the server just now to get 2001, and use this number to fill in the "Ack" field.

The client sends out the data packet and enters the ESTABLISHED state, indicating that the connection has been successfully established.

4) When the server receives the data packet and detects that only the ACK flag is set, it knows that this is the "acknowledgement packet" sent by the client, and the server will check the "acknowledgement number (Ack)" field to see if its value is 2000+1, if it is, it means that the client side has entered the successfully connected state, and the server also enters the EATABLISED state.

At this point, both the client and server enter the EATABLISED state, the connection is established successfully, and then you can send and receive data.

The key to the three-way handshake is to confirm that the other party has received its own "Seq" field.

3. The transmission process of TCP data

After the connection is established, the two hosts can transmit data to each other. As shown below:

The transmission process is as follows: First, host A sends 100 bytes of data through one data packet, and the "Seq number" field of the data packet is set to 1200. In order to confirm that it has received it, Host B wants Host A to send an ACK packet and set the "Acknowledgement Number (Ack)" field to 1301.

In order to ensure that the data arrives accurately, the target machine must immediately return an ACK packet after receiving the data packet, so that the sender can confirm the success of the data transmission.

At this time, why the Ack number is not 1200+1, but 1200+100, because the increment of the Ack number is the number of data bytes transmitted. Assuming that the number of bytes transmitted is not added to the Ack number each time, although the transmission of the data packet can be confirmed, it is not clear whether all 100 bytes are correctly transmitted or a part is lost.

Ack号 = Seq号 + 传递的字节数 + 1

Same as the three-way handshake protocol, adding 1 at the end is to tell the other party the Seq number to be transmitted.

4. Data packet loss and solutions during transmission

4.1. Packet loss

The following analyzes the situation of data packet loss during transmission, as shown in the following figure:

 

The above figure shows that 100 bytes of data are transferred to host B through the Seq1301 data packet, but an error occurred in the middle, and host B did not receive it. After a period of time, host A still has not received the ACK confirmation for Seq1301, so it tries to retransmit the data.

In order to complete the retransmission of data packets, the TCP socket will start a timer every time a data packet is sent. If the ACK packet from the target machine is not received within a certain period of time, the timer will expire and the data packet will be retransmitted.

Note that the above figure demonstrates the case of data packet loss. There will also be cases of ACK packet loss, which will also be retransmitted.

4.2. Retransmission Time Out (RTO, Retransmission Time Out)

If this value is too large, it will cause unnecessary waiting, and if it is too small, it will cause unnecessary retransmission. In theory, the network RTT time is best, but it is subject to changes in network distance and transient delay, so in practice, adaptive dynamics are used. Algorithms (Jacobson algorithm and Karn algorithm) to determine the timeout period.

RTT (Round-Trip Time) represents the total delay from the start of sending data at the sender until the sender receives the ACK confirmation packet from the receiver.

4.3, the number of retransmissions

The number of TCP packet retransmissions varies according to the system settings. In some systems, a data packet is only required to be retransmitted 3 times. If the ACK confirmation of the data packet is not received after 3 retransmissions, no retransmission is attempted. However, some demanding business systems will continuously retransmit lost data packets to ensure the normal interaction of business data as much as possible.

Finally, it needs to be explained that the data in the output buffer will be cleared only after receiving the ACK confirmation packet from the other party.

5. Wave four times (TCP disconnected)

Establishing a connection requires three handshake, each of which must return an ACK confirmation packet, and disconnecting requires four waves of hands, which can be vividly compared to the following dialogue:

  • [Shake 1] Socket A: "The task is finished, I want to disconnect."
  • [Shake 2] Socket B: "Oh, isn't it? Please wait a moment, I'll prepare it."
  • After waiting for a while...
  • [Shake 3] Socket B: "I'm ready to disconnect."
  • [Shake 4] Socket A: "Okay, thank you for your cooperation."

After the connection is established, both the client and the server are in the ESTABLISED state. At this time, the client initiates a disconnect request:

1) After the client calls the close() function, it sends a FIN packet to the server and enters the FIN_WAIT_1 state. FIN is the abbreviation of Finish, which means that the connection needs to be disconnected to complete the task.

2) After the server receives the data packet, it detects the set FIN flag and knows that it wants to disconnect, so it sends a "confirmation packet" to the client and enters the CLOSE_WAIT state.

Note: The server does not immediately disconnect the connection after receiving the request, but first sends a "confirmation packet" to the client, telling it that I know, and I need to prepare to disconnect.

3) The client enters the FIN_WAIT_2 state after receiving the "confirmation packet", and waits for the server to be ready to send the data packet again.

4) After waiting for a while, the server is ready and can be disconnected, so it actively sends a FIN packet to the client, telling it that I am ready, disconnect it, and then enter the LAST_ACK state.

5) After the client receives the FIN packet from the server, it sends an ACK packet to the server to tell it you disconnect. Then go to the TIME_WAIT state.

6) After the server receives the ACK packet from the client, it disconnects, closes the socket, and enters the CLOSED state.

A note about the TIME_WAIT state

The client enters the TIME_WAIT state after sending the ACK packet for the last time, instead of closing the connection in the CLOSED state. Why is this?

TCP is a connection-oriented transmission method. It must be ensured that data can reach the target machine correctly without loss or error. The network is unstable and may destroy data at any time. Therefore, every time machine A sends a data packet to machine B, Ask machine B to "confirm" and send back the ACK packet to tell the machine that I received it, so that machine A can know that the data transmission is successful. If machine B does not send back the ACK packet, machine A will resend it until machine B sends back the ACK packet.

When the client sends back the ACK packet to the server for the last time, the server may not receive it due to network problems. The server will send the FIN packet again. If the client completely closes the connection at this time, the server will not receive the ACK packet anyway. So the client needs to wait for a while and confirm that the other party receives the ACK packet before entering the CLOSED state. So, the new question is coming, how long will it take to wait?

Data packets have a time to live in the network. After this time, they will be discarded before reaching the target host, and the source host will be notified. This is called the Maximum Segment LifeTime (MSL, Maximum Segment LifeTime) of the message. TIME_WAIT needs to wait for two 2MSLs before entering the CLOSED state. The ACK packet reaches the server requires MSL, and the server needs MSL time to retransmit the FIN packet. 2MSL is the maximum round-trip time of the data packet. If the FIN packet retransmitted by the server has not been received after 2MSL, the server has received the ACK packet, and the client You can enter the CLOSED state.

Guess you like

Origin blog.csdn.net/weixin_40179091/article/details/113621599