当前位置:网站首页>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
边栏推荐
- Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
- 数学公式截图识别神器Mathpix无限使用教程
- 第十七周作业
- (4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
- [untitled]
- 利用LNMP实现wordpress站点搭建
- Un article traite de la microstructure et des instructions de la classe
- [screen recording] how to record in the OBS area
- Tensor attribute statistics
- Use of shell:for loop
猜你喜欢
audiopolicy
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Masked Autoencoders Are Scalable Vision Learners (MAE)
Basic knowledge of database (interview)
Detailed explanation of pointer and array written test of C language
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
数学公式截图识别神器Mathpix无限使用教程
Tensor attribute statistics
【Note17】PECI(Platform Environment Control Interface)
随机推荐
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Calculating the number of daffodils in C language
TypeError: this. getOptions is not a function
grafana工具界面显示报错influxDB Error
Idea rundashboard window configuration
Shell: operator
媒体查询:引入资源
Hcip day 12 (BGP black hole, anti ring, configuration)
Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
Basic knowledge of database (interview)
数学公式截图识别神器Mathpix无限使用教程
利用LNMP实现wordpress站点搭建
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
leecode-学习笔记
Multi camera stereo calibration
Realize reverse proxy client IP transparent transmission
Alibaba Tianchi SQL training camp task4 learning notes
Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
东南亚电商指南,卖家如何布局东南亚市场?
Week 17 homework