How to guarantee the reliable transmission of TCP

How to guarantee the reliable transmission of TCP

The system summarizes how it guarantees data transmission in a TCP connection

Insert picture description here

01 Preface


We have previously introduced that TCP connections are more complex and safer than UDP connections, but we want to know how it guarantees the security of these data? What are the secrets of sending data? Next, I will summarize these detailed questions one by one.

Insert picture description here

02 Ways to ensure data security


TCP mainly provides methods such as checksum, sequence number / acknowledgment response, timeout retransmission, maximum message length, and sliding window control to achieve reliable transmission.

Checksum

Through the checksum method, the receiving end can detect whether the data is error or abnormal, if there is an error, it will directly discard the TCP segment and resend it. When calculating the checksum, TCP adds a 12-byte pseudo header to the TCP header. The checksum calculates a total of 3 parts: TCP header, TCP data, TCP pseudo header
Insert picture description here

Serial number / confirmation response

This mechanism is similar to the form of question and answer. For example, in the classroom, the teacher will ask you "Do you understand?" If you do not respond after a period of time or if you do not understand, then the teacher will say it again. In fact, the computer's confirmation response mechanism is also the same. The sending end sends information to the receiving end, and the receiving end will respond with a packet. This packet is the response packet.

Insert picture description here

In the above process, as long as the sending end has a packet transmission, the receiving end does not respond to the confirmation packet (ACK packet), it will be retransmitted. Or the response packet of the receiving end, and the sending end will retransmit the data without receiving it. This can ensure the integrity of the data.

Timeout retransmission

Overtime retransmission refers to the time between the sent data packet and the receipt of the acknowledgment packet. If this time is exceeded, it will be considered as a packet loss and needs to be retransmitted. So how do we confirm this time value?

We know that the time of one round is always the same, and there will be a concept similar to the average. For example, it takes a total of 0.5s to send a packet to the receiving end, and then it takes 0.5s to send a confirmation packet back to the sending end. The two times are RTT (round trip time). Then there may be a problem due to network reasons, the time will have a deviation, called jitter (variance).

From the above introduction, the time for overtime retransmission is probably slightly longer than the round trip time + jitter value.

Insert picture description here

However, in the process of retransmission, if a packet has not been received by the peer after multiple retransmissions, it will be regarded as abnormal at the receiving end and the connection will be closed forcibly. And notify the application that the communication is abnormally forcibly terminated.

Maximum message length

When establishing a TCP connection, the two parties agree on a maximum length (MSS) as the unit of transmission. When retransmitting, this unit is also used for retransmission. Ideally, the data of this length is just not divided by the network layer.
Insert picture description here

Sliding window control

The overtime retransmission mechanism we mentioned above has the problem of inefficiency. It takes a period of time to send one packet to the next. So we wondered if we could send the next packet without waiting for the confirmation packet? This brings up the concept of a sliding window.

Insert picture description here

The size of the window is the maximum amount of data that the sender can send without waiting for the confirmation packet. The realization of this mechanism is to use a large number of buffers, by confirming the function of multiple segments. The next confirmation packet can determine whether the receiving end has received the data, and if it has been received, delete the data from the buffer.

The data outside the window is the data that has not been sent and the peer has received. So how does the sender judge whether the receiver has received the data? Or how do you know what data needs to be resent? Know it by the following picture.

Insert picture description here

As shown in the figure above, before receiving the serial number data that it expects, the receiving end will repeatedly confirm the previous data. After the sender receives the same response packet three times in succession, the data has been lost and needs to be resent.

Congestion control

Window control solves the problem of packet loss between the two hosts due to the transmission rate, and on the one hand ensures the reliability of TCP data transmission. However, if the network is very congested, then sending data at this time will increase the burden on the network, then the transmitted data segment may exceed the maximum survival time and not reach the receiver, which will cause packet loss. To this end, TCP introduces a slow-start mechanism, which sends out a small amount of data first, just like pathfinding, first finds out the current network congestion status, and then decides how fast to transmit data.

A congestion window is introduced here:

Define the size of the congestion window at the beginning of the transmission as 1; each time an ACK response is received, the congestion window increases by 1; and each time data is sent, the sending window takes the smallest of the congestion window and the pick-up segment reception window.

Slow start: increase exponentially at the beginning of the startup; set a slow start threshold, stop exponential growth when the exponential growth reaches the threshold, and increase to the congestion window according to the linear growth method; immediately increase the congestion window when the linear growth reaches network congestion Set it back to 1 to start a new round of "slow start", and at the same time the threshold of the new round becomes half of the original.

Insert picture description here

03 Summary


In fact, the knowledge mentioned above was also learned in school, but the seemingly useless knowledge will not be taken seriously in the future, so I will summarize it now. For the network optimization part, you can refer to the above methods for optimization. You can use these methods to provide high-speed and reliable communication services.

Reference article

  • Graphical TCP / IP
  • How does TCP guarantee data transmission

Insert picture description here

Published 57 original articles · won praise 6 · views 6419

Guess you like

Origin blog.csdn.net/weixin_42724176/article/details/104811078
Recommended