当前位置:网站首页>Interview shock 69: is TCP reliable? Why?
Interview shock 69: is TCP reliable? Why?
2022-07-29 00:57:00 【Wang Lei】
Compared with UDP Come on ,TCP Its main characteristics are three : There is a connection 、 reliable 、 Data flow oriented . So-called “ There is a connection ” refer to TCP Connection management mechanism in , That is, the famous three handshakes and four waves , It's like making a phone call , Want normal communication , You must first establish a connection with the other party , That's what's called “ There is a connection ”, We will talk about the data flow oriented mechanism later , Our topic today is :TCP How to ensure reliability ? TCP The reason why reliability can be guaranteed , Mainly through the following 6 A means :
The checksum Confirm response Over time retransmission flow control Congestion control Discard duplicate data
Next , Let's take a detailed look at the specific implementation of these means .
1. The checksum
TCP The data format of the protocol is shown in the figure below :
( picture source : Xu Rusheng xxrs) As can be seen from the above figure “ The checksum ” Is stored in the TCP A data in the header ,TCP The sending end and receiving end of will adopt the same algorithm , According to the data sent, calculate a 16 Bit checksums , And the checksum will be sent to the receiving end together with the data . After receiving the data , A new checksum will be generated based on the received data , Then compare the new checksum with the passed checksum , If the checksums are the same , Then it means that there is no change in the data transmission process , It's a valid data , Otherwise, it is invalid data , Just give up .
Basic checksum algorithm
TCP/UDP/IP The checksum algorithm of such protocols is the same , The data flow is regarded as 16 Repeat the superposition calculation for bit integer stream . To calculate the test and , First, set the inspection and field to 0, then , For each in the valid data range 16 Bit for binary inverse code summation , The results are in the tests and fields , If the data length is odd, fill in one byte 0. When you get the data , Also for each in the valid data range 16 The sum of the binary inverse code of the digits . Because the receiver includes the verification and the , therefore , If there is no error in the transmission of the first part , Then the receiver's calculation result should be all 0 Or all 1( To be specific, we have realized , The essence is the same ) . If the result is not all 0 Or all 1, That means the data is wrong .
2. Confirm response
Confirmation and response mechanism is the key means to ensure the reliability of message delivery , It is also almost all message oriented middleware (MQ) in , One of the most commonly used technologies , For example, mainstream message oriented middleware RabbitMQ、Kafka、RocketMQ There is a confirmation and response mechanism in , That's what we often say ACK(ACKnowledge Character, Confirmation character ). The confirmation response mechanism is TCP in , The core mechanism to ensure the reliability of messages . How can you confirm that the other party must have received your message ? Undoubtedly, the most effective way is for the other party to tell you , It has received , This is the confirmation response . The process of confirmation and response is shown in the following figure : 
3. Over time retransmission
There may be two problems during the confirmation of the message : First of all , The message was lost when it was sent , second , The message was lost when confirming the reply , As shown in the figure below :
obviously , Even if there is a confirmation and response mechanism, it can not guarantee that the message will not be lost , Then what shall I do? ? It doesn't matter if the message is lost , After the sender confirms the loss of the message , It is not enough to compensate the receiver for the same message ? This is the retransmission timeout mechanism .
Clever timeout retransmission mechanism
TCP The timeout retransmission mechanism of is also very clever in design , It is to ensure that messages are in any environment , Can communicate efficiently , therefore TCP It's using “ Dynamic time ” Timeout retransmission mechanism of . For example, if the message is lost for the first time , Then the sender will be 500ms Then send a message , If the second message sent is also lost , Then the sender will be 1000ms Then send a message , If the third message is also lost , Then it will 2000ms Then send a message , If you accumulate a certain number of times , The message has not been successfully sent , that TCP You will think that the other host is abnormal , Will force the connection to close , This is it. TCP The main execution process of overtime retransmission .
4. flow control
The speed of data processing at the receiving end is limited , If the sender sends too fast , This will cause the buffer at the receiving end to be full , At this time, if the sender continues to send , It will cause packet loss , Then it causes a series of chain reactions such as packet loss and retransmission . therefore TCP According to the processing of the receiving end , Dynamically adjust the size of the sent data , This mechanism is called flow control (Flow Control).
5. Congestion control
Congestion control refers to TCP According to the current network situation , Dynamically control the amount of data sent , Transfer data at a suitable speed . Imagine , If TCP In an environment that is not clear about the network , Rashly send a large amount of data to the receiving end , This will lead to more packet loss and overtime retransmission , Thus causing a series of chain reactions , Cause data transmission to slow down . and TCP What is taken is “ Slow start ” Mechanism , Start with a small amount of data , Explore the way , Find out the current state of network congestion , And then decide how fast to transmit data , This is the congestion control mechanism . If more data is transferred , There are a lot of packet losses , that TCP It will reduce the amount of data sent , Then try to slowly increase the amount of data sent , Through this form of dynamically sending data packets , To achieve data transmission suitable for the current network speed , This is it. TCP The concrete realization of congestion control .
6. Discard duplicate data
Through the previous knowledge, we know , When confirming the response , Due to the loss of acknowledgement message , Then the receiver may receive duplicate data from the sender , As shown in the figure below :
At this time, for the business side , Only one data is needed , therefore TCP There is also a mechanism , Mechanism for discarding duplicate data , This will ensure that the data received by the business party is correct . TCP A number will be added to each package sent , If a packet with the same number is received , Then it means that the receiving end gets duplicate packets , Just throw it away .
summary
TCP The main means to ensure reliability are 6 individual : The checksum 、 Confirm response 、 Over time retransmission 、 flow control 、 Congestion control 、 Discard duplicate data . Among them, flow control and congestion control are easy to confuse , We need to know clearly , Flow control is a control mechanism for the receiving capacity of the receiving end , Congestion control is a control mechanism for the current network , So don't mix it up .
Reference resources & Acknowledgement
blog.csdn.net/namelcx/article/details/6866720
It's up to you to judge right and wrong , Disdain is to listen to people , Gain or loss is more important than number .
official account :Java Analysis of the real interview questions
Interview collection :https://gitee.com/mydb/interview
边栏推荐
- Send SMS verification code asynchronously using Ronglian cloud celery
- 散列表 ~
- 会议OA项目之会议通知&会议反馈&反馈详情功能
- 直流无刷电机控制器(换电机霍尔收费多少)
- flask与七牛云上传图片
- The method of tracking the real-time market of London Silver
- Execute immediate simple sample set (DML)
- CUDA相关
- Error reporting: Rong Lianyun sends SMS verification code message 500
- Download the latest version of visual studio code and connect to the server remotely (very detailed)
猜你喜欢

Anomaly detection and unsupervised learning (2)

数仓搭建——ADS层

Download the latest version of visual studio code and connect to the server remotely (very detailed)

DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction

"Food alliance ordering system"

靠云业务独撑收入增长大梁,微软仍然被高估?

Api 接口优化的那些技巧

🧐 Table1 | finish your third line watch in one second

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告

【Web开发】Flask框架基础知识
随机推荐
"Food alliance ordering system"
Shell programming specifications and variables
SQL Server 只有数据库文件,没有日志文件,恢复数据时报1813错误的解决方案
CUDA相关
Have you seen the management area decoupling architecture? Can help customers solve big problems
主线程与守护线程
How to solve the problem that the Oracle instance cannot be started
Techo Hub 福州站干货来袭|与开发者共话工业智能新技术
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
day8
Data warehouse construction - ads floor
How to solve Oracle not available
Error reporting: when the browser clicks the modify add button, there is no response and no error reporting. Solution
iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器
Irregular clipping of NC data with CDO
zabbix部署及监控
Selenium docking agent and selenium wire access developer tool network
SurfaceControl和SurfaceFlinger通信
面试突击69:TCP 可靠吗?为什么?
[raspberry pie] how does the windows computer connect with raspberry pie