当前位置:网站首页>Solution to the packaging problem of asyncsocket long connecting rod
Solution to the packaging problem of asyncsocket long connecting rod
2022-07-05 23:15:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
project Using long connection, come on server communicate . therefore , Specify our agreement The first two bytes are data length To distinguish packets
app There are two forms of data transmission :
1.app Actively request the required data ;
2.app Asynchronously receive push messages from the server , That is to say app There is no request here . The server actively sends data to appclient.
Whole app During execution , They all transmit data on the same connection . Therefore, the following problems will appear :
1.server Data transmission is too fast , There is a problem of sticking the package , such as
1.1 The server sends multiple push messages at a time ;
1.2 Network instability ,client Send multiple requests in succession client Receive all replies at once .
2.client A request message , The response message data of the server is too large . To IP The layer needs to be divided , therefore client There will be several times before receiving the complete data ;
First, there are the following 4 Three methods need to be introduced
/**
** Example method
** After calling this method , When the socket receive buffer has available bytes . Will trigger onSocket:didReadData:withTag: Payment method . At this time, the problems mentioned above will appear in the received data
*/
– (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;
/**
** Example method
** After calling this method . When socket receive buffer has length Length of available bytes , Will trigger onSocket:didReadData:withTag: Payment method , At this time, fixed length data is received , This fixed length is length Given value , When length When the length of is greater than the length of the receiving buffer , Will wait , Until received length The call of the above delegated method will be triggered when the length of the data
*/
– (void)readDataToLength:(NSUInteger)length withTimeout:(NSTimeInterval)timeout tag:(long)tag;
/**
** Example method
** The function of this method is the same as above , Just a few more parameters ,buffer Is where you write the received data ,offset It's about writing buffer Offset position in
*/
– (void)readDataToLength:(NSUInteger)length
withTimeout:(NSTimeInterval)timeout
buffer:(NSMutableData *)buffer
bufferOffset:(NSUInteger)offset
tag:(long)tag;
/**
** Payment method
** This method has been mentioned above
*/
– (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)_data withTag:(long)tag.
resolvent :
client After each request is sent . First, only two bytes of length are received , For example, the following :
[sendSocket readDataToLength:2 withTimeout:set.timeout tag:tag];[sendSocket writeData:data withTimeout:set.timeout tag:tag];
Then, when available bytes reach the socket receive buffer, the following delegated method is triggered , We do the following processing in it , This solves the problem of sticking bags , It also overcomes the problem that the data is too large . Receive complete questions multiple times ;
- (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];
}
Copyright notice : This article is an original blog article . Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117542.html Link to the original text :https://javaforall.cn
边栏推荐
- Design and implementation of secsha system
- leecode-学习笔记
- Element positioning of Web Automation
- [speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
- Sum of two numbers, sum of three numbers (sort + double pointer)
- Object detection based on impulse neural network
- Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
- 使用rewrite规则实现将所有到a域名的访问rewrite到b域名
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
- Leecode learning notes
猜你喜欢
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Getting started stm32--gpio (running lantern) (nanny level)
透彻理解JVM类加载子系统
Three. JS VR house viewing
终于搞懂什么是动态规划的
Alibaba Tianchi SQL training camp task4 learning notes
CJ mccullem autograph: to dear Portland
Thoroughly understand JVM class loading subsystem
Object detection based on impulse neural network
一文搞定垃圾回收器
随机推荐
并查集实践
YML configuration, binding and injection, verification, unit of bean
fibonacci search
TOPSIS code part of good and bad solution distance method
grafana工具界面显示报错influxDB Error
Multi view 3D reconstruction
UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
数学公式截图识别神器Mathpix无限使用教程
[screen recording] how to record in the OBS area
Debian 10 installation configuration
Using LNMP to build WordPress sites
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Hcip day 11 (BGP agreement)
一文搞定垃圾回收器
Vision Transformer (ViT)
openresty ngx_ Lua request response
Practice of concurrent search
查看网页最后修改时间方法以及原理简介
Registration and skills of hoisting machinery command examination in 2022