当前位置:网站首页>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)]:
边栏推荐
- 注释、接续、转义等符号
- Postman pre script - global variables and environment variables
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Excel转换为Lua的配置文件
- jdbc使用call调用存储过程报错
- Codeforces Round #804 (Div. 2)
- 趋势前沿 | 达摩院语音 AI 最新技术大全
- Using stopwatch to count code time
- 剑指 Offer II 039. 直方图最大矩形面积
- Nacos - TC Construction of High available seata (02)
猜你喜欢
Hyperledger Fabric2. Some basic concepts of X (1)
Simple understanding of interpreters and compilers
RT thread analysis log system RT_ Kprintf analysis
Configuration file converted from Excel to Lua
Class inheritance in yyds dry inventory C
Using stopwatch to count code time
idea一键导包
趋势前沿 | 达摩院语音 AI 最新技术大全
Questions d'examen écrit classiques du pointeur
flutter 实现一个有加载动画的按钮(loadingButton)
随机推荐
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
Drive development - the first helloddk
Safe mode on Windows
Postman manage test cases
Knowledge points of circular structure
Zoom and pan image in Photoshop 2022
驱动开发——第一个HelloDDK
图论的扩展
Nestjs配置文件上传, 配置中间件以及管道的使用
Summary of three log knowledge points of MySQL
Hometown 20 years later (primary school exercises)
Request (request object) and response (response object)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Codeforces Round #804 (Div. 2)
UCF (summer team competition II)
Huawei od computer test question 2
Figure database ongdb release v-1.0.3
[leetcode16] the sum of the nearest three numbers (double pointer)
Collection + interview questions
Fuzzy -- basic application method of AFL