当前位置:网站首页>TCP的粘包拆包问题+解决方案
TCP的粘包拆包问题+解决方案
2022-07-27 21:01:00 【知识浅谈】
为什么TCP有而UDP没有粘包
1️⃣因为udp的数据包有保护边界。
2️⃣tcp是以字节流的形式,也就是没有边界,所以应用层的数据在传输层的时候就可能会出现粘包和拆包问题。
出现这种问题的原因图解
1️⃣字节流可以理解为一个双向的通道里流淌的数据,这个数据其实就是我们常说的二进制数据,简单来说就是一大堆 01 串。这些 01 串之间没有任何边界。
2️⃣应用层传到 TCP 协议的数据,不是以消息报为单位向目的主机发送,而是以字节流的方式发送到下游,这些数据可能被切割和组装成各种数据包,接收端收到这些数据包后没有正确还原原来的消息,因此出现粘包现象。
粘包情况
要发送的数据小于TCP发送缓冲区的大小,TCP将多次写入缓冲区的数据一次发送出去,将会发生粘包;

拆包情况
要发送的数据大于TCP发送缓冲区剩余空间大小,将会发生拆包;

拆包&粘包

解决方案
应用层措施
1️⃣封装固定包的大小。
2️⃣在包的结尾加上固定的字符,比如:FTP中的解决方法:末尾加上\r\n
3️⃣消息头+消息体。消息头中有消息体的长度。
4️⃣自定义。
边栏推荐
- In 2019, the world's top ten semiconductor manufacturers: Intel returned to the first place, and apple rose sharply against the trend
- [signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
- 怎么使用xshell免费版
- Winform怎么使用FTP实现自动更新
- Redis hash underlying data structure
- Introduction to several common usage scenarios of message queue
- Which one is better to request to merge -- three skills of interface request merging, and the performance directly explodes the table
- 记录一下使用R语言中关于formatC的错误
- 突发,微信重要通知
- 解决5G使用痛点,魅族17 mSmart 5G快省稳技术发布
猜你喜欢

强化学习——PyTorch 实现 Advantage Actor-Critic (A2C)

用3dmax做折扇的思路方法与步骤

proteus仿真arduino中调用DHT11/22温湿度传感器
![[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code](/img/a7/8f0d929457d285adc8020c7f5d33ea.png)
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code

一位软件投资者的独白:我为什么不追逐快速增长的公司
![[image defogging] image defogging based on dark channel and non-mean filtering with matlab code](/img/44/6120682f9571f6ad35f8b9249b7fea.png)
[image defogging] image defogging based on dark channel and non-mean filtering with matlab code

CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021

习题 --- BFS

My annual salary is 1million, and I don't have clothes more than 100 yuan all over my body: saving money is the top self-discipline

Calling dht11/22 temperature and humidity sensor in Proteus simulation Arduino
随机推荐
Redis hash underlying data structure
Your list is too laggy? These four optimizations can make your list silky smooth
Pentium fast system call learning
Nail alarm tool
采用汇顶屏下光学指纹方案,三星Galaxy A71 5G上市
Learn more about xxE injection
What is the MySQL data storage method?
怎么使用C# Winform实现复制文件显示进度
图基本知识代码
The print version of imeta | international standard ISSN is officially confirmed, and the application for dual ISSN is completed
76000 people shut down! Toshiba announced the closure of all factories in Japan
urllib.error. URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: un
CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
reduce错误示范
Winform怎么使用FTP实现自动更新
[number recognition] recognize 0-9 numbers based on Hopfield neural network with matlab code
What are the methods of process synchronization?
TSMC 3nm detail exposure: transistor density as high as 250million /mm ², Greatly improved performance and energy efficiency
Nature综述:微生物群落形成过程中的优先效应
Those "experiences and traps" in the data center
