当前位置:网站首页>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
边栏推荐
- The maximum happiness of the party
- Déterminer si un arbre binaire est un arbre binaire complet
- Multi view 3D reconstruction
- Week 17 homework
- Selenium+Pytest自动化测试框架实战
- Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
- CJ mccullem autograph: to dear Portland
- Summary of binary tree recursive routines
- openresty ngx_lua正则表达式
- 二叉树递归套路总结
猜你喜欢

一文搞定JVM的内存结构

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

Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal

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

Debian 10 installation configuration

Go language implementation principle -- map implementation principle

Activate function and its gradient

东南亚电商指南,卖家如何布局东南亚市场?

LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
![[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]](/img/03/8fa104b177698a15b7ffa70d4fb524.jpg)
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
随机推荐
From the perspective of quantitative genetics, why do you get the bride price when you get married
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Déterminer si un arbre binaire est un arbre binaire complet
Common JVM tools and optimization strategies
两数之和、三数之和(排序+双指针)
Openresty ngx Lua regular expression
2022.02.13 - SX10-30. Home raiding II
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
Initial experience | purchase and activate typora software
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
SPSS analysis of employment problems of college graduates
Nacos installation and service registration
Three. Js-01 getting started
Common model making instructions
Three.JS VR看房
Object detection based on impulse neural network
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
媒体查询:引入资源
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
Negative sampling