当前位置:网站首页>AsyncSocket长连接棒包装问题解决
AsyncSocket长连接棒包装问题解决
2022-07-05 23:02:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
project正在使用长连接快来server沟通。因此,指定我们的协议前两个字节为数据长度来区分数据包
app这边数据有两种传输形式:
1.app主动请求所须要的数据;
2.app异步接收来自服务端的推送消息,也就是app这边没有请求。服务端主动发送数据到appclient。
整个app执行期间,它们都是在同一个连接上完毕的传输数据。因此会出现下面的问题:
1.server传输数据过快,出现粘包的问题,比如
1.1服务端一次发来多个推送消息;
1.2网络不稳定,client连续发送多个请求client一次接收到所有答复。
2.client的一个请求报文,服务端的应答报文数据过大。到IP层须要进行分片,因此client这边就会出现几次才接收到完整的数据的情况;
首先有下面4个方法须要介绍
/**
**实例方法
**调用此方法以后,当套接字接收缓冲区有可用字节的时候。会触发onSocket:didReadData:withTag:托付方法。此时接收到的数据会出现上面说到的问题
*/
– (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;
/**
**实例方法
**调用此方法以后。当套接字接收缓冲区有length长度的可用字节的时候,会触发onSocket:didReadData:withTag:托付方法,此时接收到固定长度的数据,这个固定长度就是length给出的值,当length的长度大于接收缓冲区数据的长度的时候,就会等待,直到接收到length长度的数据的时候才会触发以上托付方法的调用
*/
– (void)readDataToLength:(NSUInteger)length withTimeout:(NSTimeInterval)timeout tag:(long)tag;
/**
**实例方法
**此方法功能同上,仅仅是多了几个參数,buffer是你将接收的数据写到的地方,offset是写到buffer中的偏移位置
*/
– (void)readDataToLength:(NSUInteger)length
withTimeout:(NSTimeInterval)timeout
buffer:(NSMutableData *)buffer
bufferOffset:(NSUInteger)offset
tag:(long)tag;
/**
**托付方法
**此方法上面已经说到
*/
– (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)_data withTag:(long)tag。
解决方法:
client每次发送请求以后。首先仅仅接收两个字节的长度字节,例如以下:
[sendSocket readDataToLength:2 withTimeout:set.timeout tag:tag];[sendSocket writeData:data withTimeout:set.timeout tag:tag];然后当有可用字节到达套接字接收缓冲区的时候触发下面托付方法,我们在里面做例如以下处理,这样既攻克了粘包的问题,也攻克了数据过大。多次接收完整的问题;
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)_data withTag:(long)tag
{
SettingData* set = [SettingData shareSettingData];
if (respondData == nil) {
respondData = [[NSMutableData alloc]init];
respondDataLen = [RequestUnit respondMessageLengthWithData:_data];
[sock readDataToLength:respondDataLen withTimeout:set.timeout tag:tag];
return;
}
[respondData appendData:[RequestUnit respondBytesToUTF8Data:_data]];
[self parserData:respondData withTag:tag];
}版权声明:本文博客原创文章。博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117542.html原文链接:https://javaforall.cn
边栏推荐
- audiopolicy
- (4) UART application design and simulation verification 2 - TX module design (stateless machine)
- YML configuration, binding and injection, verification, unit of bean
- Hcip day 12 (BGP black hole, anti ring, configuration)
- 派对的最大快乐值
- Common JVM tools and optimization strategies
- Practice of concurrent search
- The maximum happiness of the party
- openresty ngx_lua正则表达式
- Multi camera stereo calibration
猜你喜欢

利用LNMP实现wordpress站点搭建

One article deals with the microstructure and instructions of class

Element operation and element waiting in Web Automation

CJ mccullem autograph: to dear Portland

openresty ngx_ Lua request response

【Note17】PECI(Platform Environment Control Interface)

实现反向代理客户端IP透传

东南亚电商指南,卖家如何布局东南亚市场?

Alibaba Tianchi SQL training camp task4 learning notes

PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
随机推荐
Go language implementation principle -- map implementation principle
Common JVM tools and optimization strategies
Week 17 homework
【Note17】PECI(Platform Environment Control Interface)
Sum of two numbers, sum of three numbers (sort + double pointer)
Thoroughly understand JVM class loading subsystem
一文搞定垃圾回收器
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
利用LNMP实现wordpress站点搭建
Un article traite de la microstructure et des instructions de la classe
openresty ngx_lua正則錶達式
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
Openresty ngx Lua regular expression
2:第一章:认识JVM规范1:JVM简介;
3:第一章:认识JVM规范2:JVM规范,简介;
Go language implementation principle -- lock implementation principle
Detailed explanation of pointer and array written test of C language
Déterminer si un arbre binaire est un arbre binaire complet
Initial experience | purchase and activate typora software
The maximum happiness of the party