当前位置:网站首页>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
边栏推荐
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
- 两数之和、三数之和(排序+双指针)
- [untitled]
- Dynamic memory management (malloc/calloc/realloc)
- 一文搞定class的微觀結構和指令
- Marginal probability and conditional probability
- Using LNMP to build WordPress sites
- Use of grpc interceptor
- Selenium+pytest automated test framework practice
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
猜你喜欢

Fix the memory structure of JVM in one article

2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines

Negative sampling

SPSS analysis of employment problems of college graduates

LeetCode102. Sequence traversal of binary tree (output by layer and unified output)

终于搞懂什么是动态规划的

如何快速理解复杂业务,系统思考问题?

audiopolicy

Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?

【原创】程序员团队管理的核心是什么?
随机推荐
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
并查集实践
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
Finally understand what dynamic planning is
2: Chapter 1: understanding JVM specification 1: introduction to JVM;
Ultrasonic sensor flash | LEGO eV3 Teaching
Nacos installation and service registration
Vision Transformer (ViT)
二叉树递归套路总结
audiopolicy
Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
Negative sampling
Error when LabVIEW opens Ni instance finder
CJ mccullem autograph: to dear Portland
February 13, 2022-4-symmetric binary tree