当前位置:网站首页>Detailed explanation of TCP (3)
Detailed explanation of TCP (3)
2022-07-31 03:02:00 【Javanese Chicken @】
Article table of contents
9. Byte stream oriented
In this case of byte stream oriented, there is an important problem to pay attention to: the sticky packet problem (referring to the application side datagram)
For TCP, the data is transmitted as shown below


But for UDP, since UDP transmits datagrams, it is transmitted one by one


Then the question is, how to solve the sticky package problem?
Solve by designing a reasonable application layer protocol!!!
1. Set "terminator"/"delimiter" for application layer data
2. Set "length" for application layer data
Method 1:

Method 2:

10.Some exceptions in TCP
Common exceptions:
(1) Process termination
No matter how the process is terminated, the corresponding PCB will be released in essence, and the corresponding file descriptor will also be released, which will also trigger the wave four times.
"Process termination" does not mean that the connection is terminated. Process termination is actually equivalent to calling socket.close().
(2) Machine restart
When the machine restarts, it actually kills the process first, still waving four times.
(3) The machine is powered off/the network cable is disconnected
Unexpected!!The machine is too late to do anything
<1>It is the receiver who loses power
At this time, the other side is still sending data. Obviously, the sender will no longer have ACK at this time!
So the timeout is retransmitted. After several retransmissions, it will try to reset the connection and reset the segment.Then the sender will give up the connection, and the resources corresponding to the connection will be recycled.

<2>It is the sender who loses power
At this time, the other party is trying to receive data, but no data is received at this time
How does the receiver know that the sender is dead?Or is it that the sender hasn't sent it yet?
The strategy adopted by the receiver at this time is the "heartbeat packet" mechanism (also called "keep alive")
Every once in a while, send a PING packet to the other party and expect the other party to return a PONG packet.
If the PING packet has been sent, your bureau has no PONG after crossing the river, and you can't retry several times, it is considered that the other party has hung up.
Classic Interview Questions
How to achieve reliable transmission based on UDP protocol?
This question seems to be asking about UDP, but it is actually a TCP test!!!
1. Implement the confirmation response mechanism. After each data is received, Daou will feedback an ACK (this time this is not returned by the kernel, but the application defines an ACK packet and sends it back).
2. Realize serial number/confirm serial number, and realize deduplication.
3. Realize timeout retransmission.
4. Implement connection management.
5. To improve efficiency, implement a sliding window.
6. To limit the sliding window, implement flow control/congestion control.
7. Realize delayed response, piggyback response, heartbeat mechanism...
Which scenario is suitable to use TCP and which scenario is suitable to use UDP?
1. If reliability is required, TCP is preferred.
2. If the single data table of the number of beds is long (more than 64K), TCP is still used.
3. If you pay special attention to efficiency, give priority to UDP.
Typical scenario: communication between hosts in a computer room.
4. If broadcasting is required, UDP is preferred.
A piece of data is sent to multiple hosts at the same time. UDP itself supports broadcasting, but TCP itself does not support it. It can only send data to each host through multiple connections and polling in the application layer program.(pseudo broadcast).
边栏推荐
猜你喜欢
随机推荐
Graphical lower_bound & upper_bound
【Cocos Creator 3.5】缓动系统停止所有动画
return in try-catch
Pythagorean tuple od js
Number 16, top posts
16、热帖排行
分布式与集群是什么 ? 区别是什么?
字体压缩神器font-spider的使用
C primer plus学习笔记 —— 8、结构体
YOLOV5 study notes (3) - detailed explanation of network module
什么是分布式锁?实现分布式锁的三种方式
YOLOV5学习笔记(三)——网络模块详解
全流程调度——MySQL与Sqoop
Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
String为什么不可变?
19.支持向量机-优化目标和大间距直观理解
The difference between link and @import
C#远程调试








