当前位置:网站首页>Solution of QT TCP packet sticking
Solution of QT TCP packet sticking
2022-07-06 05:18:00 【hitzsf】
1. Why? TCP There will be sticky subcontracting and half subcontracting ?
TCP There are many reasons for the phenomenon of sticking and subcontracting , It may be caused by the sender , It may also be caused by the receiver .
The cause of the sender is caused by TCP The agreement itself
TCP To improve transmission efficiency , The sender often needs to collect enough data before sending a packet of data . If the data sent several times in succession is very small , Usually TCP According to the optimization algorithm, these data will be synthesized into a packet and sent out at a time , The receiver then receives the packet data . The advantages are obvious , It is to reduce the number of small packets in the WAN , So as to reduce the emergence of network congestion . In general : The sender sent data several times , The receiver reads all the data at one time , Cause multiple sending and one reading ; Usually network traffic optimization , Fill up several small data segments to a certain amount of data , Thus, the transmission times in the network link are reduced .
The reason caused by the receiver is that the user process of the receiver does not receive data in time , This leads to the phenomenon of sticking .
Because the receiving party first puts the received data in the system receiving buffer , The user process fetches data from this buffer , If the data of the previous packet has not been taken away by the user process when the next packet arrives , When the next packet is placed in the receiving buffer of the system, it will be received after the previous packet , The user process receives data from the system according to the preset buffer size , In this way, multiple packets of data are fetched at one time .
Subcontracting or semi subcontracting refers to that our receiver should subcontract when sticky packets occur .
Subcontracting phenomenon in A long connection Will appear in . In general : The sender sent a large amount of data , When the receiving end reads the data, the data arrives in batches , Cause one send and multiple reads ( In practice , Client on TCP_NODELAY after , The server is still contaminated , So here is sending multiple times and reading once ); It is usually related to the cache size of network routes , The size of a data segment exceeds the cache size , Then unpack and send .
2. How to deal with the phenomenon of sticking and subcontracting ?
tcp Sticky package 、 Half package processing :
One is to use separator , Use a special separator as the end of a packet , for example :”$$”;
The second is to adopt a specific location for each package ( For example, the first two bytes of the packet ) Add the length information of the packet , After the other end receives the data, it intercepts the data of a specific length according to the length of the data packet for analysis .
3. Qt Handle sticky and half wrapped demo
For simple display , Use QList< QByteArray > To simulate the socket Receive multiple data , And a complete packet with “$$” end , for example :1111$$
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
// Simulate sticky packet and half packet data
QList<QByteArray> recvMsgs = {
"1111$$2222$$xxxxx","3333$$444$$yyyy"};
static QByteArray halfData="0000"; // Half packet data left over from the last time
for (int var = 0; var < recvMsgs.size (); ++var) {
auto recvMsg = recvMsgs.at (var);
// Package data
int idx = 0;
while (idx != -1) {
// from idx Location start search $$ The location of
int postion = recvMsg.indexOf ("$$",idx); // Solve the sticking problem "1111$$2222$$xxxxx"
if(postion != -1) {
// obtain $$ Data before , barring $$
auto byte = recvMsg.mid(idx, postion - idx);
if(!halfData.isEmpty ()){
byte = halfData+byte;
halfData.clear ();
}
qDebug() << byte;
postion += 2;
}
else{
// If there is a half bag phenomenon , Then put the last "$$" Save the data after , Temporarily not used
if(idx < recvMsg.length ()){
halfData = recvMsg.mid (idx);
}
}
idx = postion;
}
qDebug() << " The remaining half packet of data :" << halfData;
}
}
Running results :
"00001111"
"2222"
The remaining half packet of data : "xxxxx"
"xxxxx3333"
"444"
The remaining half packet of data : "yyyy"
Reference applications :
[tcp Sticky package ( One ) - silyvin - Blog Garden (cnblogs.com)]:
边栏推荐
- 指針經典筆試題
- Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
- Modbus协议通信异常
- 组播和广播的知识点梳理
- Fluent implements a loadingbutton with loading animation
- Raspberry pie 3.5-inch white screen display connection
- Set detailed map + interview questions
- Force buckle 1189 Maximum number of "balloons"
- Idea one key guide package
- Unity Vector3. Use and calculation principle of reflect
猜你喜欢
Huawei equipment is configured with OSPF and BFD linkage
[classic example] binary tree recursive structure classic topic collection @ binary tree
Modbus protocol communication exception
Compilation and connection of shader in games202 webgl (learn from)
C进阶-数据的存储(上)
Fuzzy -- basic application method of AFL
注释、接续、转义等符号
[leetcode] 18. Sum of four numbers
Please wait while Jenkins is getting ready to work
nacos-高可用seata之TC搭建(02)
随机推荐
Postman Association
A little knowledge of CPU, disk and memory
Weng Kai C language third week 3.1 punch in
Set detailed map + interview questions
關於Unity Inspector上的一些常用技巧,一般用於編輯器擴展或者其他
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
Safe mode on Windows
Promotion hung up! The leader said it wasn't my poor skills
Force buckle 1189 Maximum number of "balloons"
Review of double pointer problems
2021RoboCom机器人开发者大赛(初赛)
[mask requirements of OSPF and Isis in multi access network]
[classic example] binary tree recursive structure classic topic collection @ binary tree
Hometown 20 years later (primary school exercises)
JS quick start (II)
GAMES202-WebGL中shader的编译和连接(了解向)
毕业设计游戏商城
Summary of redis AOF and RDB knowledge points
Nacos - TC Construction of High available seata (02)
2021 robocom world robot developer competition - undergraduate group (semi-finals)