当前位置:网站首页>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
边栏推荐
- Use of shell:for loop
- Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
- Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
- Ultrasonic sensor flash | LEGO eV3 Teaching
- 【Note17】PECI(Platform Environment Control Interface)
- Basic knowledge of database (interview)
- LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
猜你喜欢
Basic knowledge of database (interview)
一文搞定垃圾回收器
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
Non rigid / flexible point cloud ICP registration
Expectation, variance and covariance
一文搞定class的微观结构和指令
数学公式截图识别神器Mathpix无限使用教程
Masked Autoencoders Are Scalable Vision Learners (MAE)
Ultrasonic sensor flash | LEGO eV3 Teaching
【Note17】PECI(Platform Environment Control Interface)
随机推荐
Déterminer si un arbre binaire est un arbre binaire complet
Hj16 shopping list
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Un article traite de la microstructure et des instructions de la classe
Yiwen gets rid of the garbage collector
What is the process of building a website
openresty ngx_ Lua regular expression
秒杀系统的设计与实现思路
【Note17】PECI(Platform Environment Control Interface)
Selenium+pytest automated test framework practice
Use of shell:for loop
证明 poj 1014 模优化修剪,部分递归 有错误
Three. JS VR house viewing
Simple and beautiful method of PPT color matching
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
Codeforces Global Round 19
Shell: operator
Fix the memory structure of JVM in one article
grafana工具界面显示报错influxDB Error
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines