当前位置:网站首页>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️⃣自定义。
边栏推荐
猜你喜欢

西门子PLC能否实时无线采集多处从站模拟量数据?
![[soft test software evaluator] 2014 comprehensive knowledge over the years](/img/85/46c8fc696ae5249a9aca22b50493f0.jpg)
[soft test software evaluator] 2014 comprehensive knowledge over the years

Flink怎么使用Savepoint

Normality of confidence interval

他山之石 | 蚂蚁超大规模知识图谱构建及应用
![[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

Spark 离线开发框架设计与实现

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

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

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
随机推荐
Interviewer: let's talk about the specific process of network data transmission
MapReduce(三)
用3dmax做折扇的思路方法与步骤
Yijia will release ODM orders in 2020 and make efforts in the middle and low-end market
主数据管理理论与实践
进程同步的方式有哪些?
[number recognition] recognize 0-9 numbers based on Hopfield neural network with matlab code
三次握手的Socket交互流程
Common Taylor expansion
What technology is RPA process automation robot? How to realize office automation?
Preliminary understanding of Panda3D audio and advanced interactive components
Redis 哈希Hash底层数据结构
Using the optical fingerprint scheme under the huiding screen, Samsung Galaxy a71 5g is listed
CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021
【GNN报告】加拿大蒙特利尔唐建:Geometric Deep Learning For Drug Discovery
四次挥手的Socket交互流程
ZCMU--1720: 死亡如风,我要装逼
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
Elk log analysis system installation and deployment
Find and leverage xxE – XML external entity injection
