当前位置:网站首页>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️⃣自定义。
边栏推荐
- 【GNN报告】加拿大蒙特利尔唐建:Geometric Deep Learning For Drug Discovery
- What are the advantages of Tita's OKR system over other similar products or shared documents?
- In 2019, the world's top ten semiconductor manufacturers: Intel returned to the first place, and apple rose sharply against the trend
- 强化学习——PyTorch 实现 Advantage Actor-Critic (A2C)
- Sudden, wechat important notice
- 【C语言】通讯录(动态版本)
- Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (5) - detailed explanation of fiddler monitoring panel
- 7.6万人停工!东芝宣布关闭日本所有工厂
- 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
- The technology of applet container is very promising, which can greatly improve the efficiency of mobile R & D
猜你喜欢

Normality of confidence interval

华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点

详解分布式系统的幂等

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

【C语言】通讯录(动态版本)

Date的使用

远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug

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

真的很难理解?RecyclerView 缓存机制到底是几级缓存?

Character stream learning 14.3
随机推荐
Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
Application of user portrait in precise push of wechat official account of scientific journals
Nature review: preferential effects in the formation of microbial communities
怎么使用xshell免费版
Exercise --- BFS
我年薪100万,全身上下没有超过100块的衣服:存钱,是最顶级的自律
Figure basic knowledge code
突发,微信重要通知
XML external entity (xxE) vulnerability and its repair method
TFRecord的Shuffle、划分和读取
Apple releases new iPhone se: equipped with A13 bionic processor, priced from 3299 yuan
ZCMU--1720: 死亡如风,我要装逼
2022/7/26
华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点
Reinforcement learning - pytorch realizes advantage actor critical (A2C)
解决5G使用痛点,魅族17 mSmart 5G快省稳技术发布
Current situation and future of Nb IOT industry: cross the threshold of 100million shipments and rush to 5g connection!
日产1500万只!比亚迪口罩拿下美国加州10亿美元订单
三次握手的Socket交互流程
【C语言】通讯录(动态版本)
