当前位置:网站首页>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
边栏推荐
- Codeforces Global Round 19
- Development specification: interface unified return value format [resend]
- 2.13 summary
- Object detection based on impulse neural network
- Leetcode buys and sells stocks
- 如何快速理解复杂业务,系统思考问题?
- 一文搞定JVM的内存结构
- Sum of two numbers, sum of three numbers (sort + double pointer)
- 数据库基础知识(面试)
- TOPSIS code part of good and bad solution distance method
猜你喜欢

2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions

数据库基础知识(面试)

Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework

一文搞定class的微观结构和指令

Go language implementation principle -- map implementation principle

February 13, 2022-4-symmetric binary tree

CJ mccullem autograph: to dear Portland

PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )

Finally understand what dynamic planning is

Hcip day 11 (BGP agreement)
随机推荐
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Leetcode sword finger offer brush questions - day 21
Debian 10 installation configuration
From the perspective of quantitative genetics, why do you get the bride price when you get married
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
Using LNMP to build WordPress sites
Getting started stm32--gpio (running lantern) (nanny level)
Use of metadata in golang grpc
【Note17】PECI(Platform Environment Control Interface)
Three. JS VR house viewing
Un article traite de la microstructure et des instructions de la classe
What is the process of building a website
Openresty ngx Lua regular expression
6-axis and 9-axis IMU attitude estimation
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
Basic knowledge of database (interview)
Thoroughly understand JVM class loading subsystem
判断二叉树是否为完全二叉树