当前位置:网站首页>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️⃣自定义。
边栏推荐
- 低代码开发前景如何,大家都真的看好低代码开发吗?
- NB-IoT产业的现状与未来:跨过1亿出货门槛,奔向5G大连接!
- Apple releases new iPhone se: equipped with A13 bionic processor, priced from 3299 yuan
- Arm32进行远程调试
- Using the optical fingerprint scheme under the huiding screen, Samsung Galaxy a71 5g is listed
- 采用汇顶屏下光学指纹方案,三星Galaxy A71 5G上市
- Preliminary understanding of Panda3D audio and advanced interactive components
- 15million per day! BYD masks won a US $1billion order in California
- 编辑复制粘贴判定问题(bug?),所见即所得显示符号问题反馈。
- Character stream learning 14.3
猜你喜欢
![[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery](/img/ef/aa490aeff5a0690257cd6eca7d5e28.png)
[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery

【GNN报告】加拿大蒙特利尔唐建:Geometric Deep Learning For Drug Discovery
![[soft test software evaluator] 2014 comprehensive knowledge over the years](/img/85/46c8fc696ae5249a9aca22b50493f0.jpg)
[soft test software evaluator] 2014 comprehensive knowledge over the years
![[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

See how Gan controls the image generation style step by step? Explain the evolution process of stylegan in detail

Introduction to several common usage scenarios of message queue

Technical certification | Tupo software and Huawei cloud create a new situation of win-win cooperation

【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)

如何快捷地查看H.265视频播放器EasyPlayer的API属性及其使用方法?

Lua基础语法学习
随机推荐
Remotely debug idea, configure remote debug, and add JVM startup parameter -xdebug in the program of remote server
Huawei Hongmeng 3 was officially released, and this security feature has solved a major pain point
smartRefresh嵌套多个RecycleView滑动冲突及布局显示不全
四次挥手的Socket交互流程
7.6万人停工!东芝宣布关闭日本所有工厂
29.学习Highcharts 使用百分比的堆叠柱形图
This is the most concise guide to tcpdump in history. It's enough to read this one
小程序容器技术超有料,可以让移动研发效率大幅提升
[CVA valuation training camp] how to quickly read the annual reports of listed companies - Lesson 4
Introduction to several common usage scenarios of message queue
Redis 哈希Hash底层数据结构
After returning to mixlab for three days, "creative team" cured my spiritual internal friction
三次握手的Socket交互流程
携手长江存储,江波龙推出全球最小扩展卡
【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
日产1500万只!比亚迪口罩拿下美国加州10亿美元订单
Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (5) - detailed explanation of fiddler monitoring panel
Solve 5g pain points, Meizu 17 smart 5g fast and stable technology release
Socket interaction process of three handshakes
用户画像在科技期刊微信公众号精准推送中的应用
