当前位置:网站首页>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)]:
边栏推荐
- Realize a binary read-write address book
- [noip2009 popularization group] score line delimitation
- Postman test report
- MySQL if and ifnull use
- Promotion hung up! The leader said it wasn't my poor skills
- MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
- 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
- 你需要知道的 TCP 三次握手
- 驱动开发——HelloWDM驱动
- GAMES202-WebGL中shader的编译和连接(了解向)
猜你喜欢

Pix2pix: image to image conversion using conditional countermeasure networks

Postman assertion

Idea one key guide package

SQLite add index

初识CDN

Simple understanding of interpreters and compilers

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example

flutter 实现一个有加载动画的按钮(loadingButton)

Yolov5 tensorrt acceleration

注释、接续、转义等符号
随机推荐
Flody的应用
Raspberry pie 3.5-inch white screen display connection
RT thread analysis log system RT_ Kprintf analysis
Qt TCP 分包粘包的解决方法
In 2022, we must enter the big factory as soon as possible
Tetris
C进阶-数据的存储(上)
Golang -- TCP implements concurrency (server and client)
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
[buuctf.reverse] 159_[watevrCTF 2019]Watshell
Mongodb basic knowledge summary
idea一键导包
Unity gets the width and height of Sprite
Postman assertion
Huawei equipment is configured with OSPF and BFD linkage
MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
RT thread analysis - object container implementation and function
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
剑指 Offer II 039. 直方图最大矩形面积
Some common skills on unity inspector are generally used for editor extension or others