Project is using long connections to quickly communicate with the server. Therefore, specifying the first two bytes of our protocol is the data length to differentiate the packet
There are two types of data transmission in this app:
1.app the data required for the unsolicited request;
2.app asynchronously receives push messages from the server, which is not requested on the app side. The server actively sends the data to the appclient.
During the execution of the app, they all transmit data that is completed on the same connection. Therefore, the following problem occurs:
1.server transmission of data too fast, the problem of sticky packets, such as
1.1 server sends multiple push messages at a time;
1.2 The network is unstable, the client sends multiple requests continuously to receive all replies at once.
2.client a request message, the service side of the response message data is too large. To the IP layer needs to be fragmented, so the client side will appear several times to receive the full data situation;
First of all, there are 4 methods that need to be introduced
/**
* * Instance method
* * After calling this method, when the socket receive buffer has available bytes. Triggers OnSocket:didReadData:withTag: The method of entrusting. The data received at this point will appear in the above mentioned problem
*/
-(void) Readdatawithtimeout: (nstimeinterval) timeout tag: (long) tag;
/**
* * Instance method
* * After calling this method. when the socket receive buffer has a length of free bytes, it will trigger the OnSocket:didReadData:withTag: The Trust method, at this time receive a fixed length of data, the fixed length is the value given by the number, When length is longer than the length of the receive buffer data, it waits until the length of the data is received, triggering the invocation of the above method.
*/
-(void) Readdatatolength: (nsuinteger) Length withtimeout: (nstimeinterval) timeout tag: (long) tag;
/**
* * Instance method
* * This method function as above, just a few more parameters ,buffer is where you write the data you receive,offset is the position that you write to the buffer.
*/
-(void) Readdatatolength: (nsuinteger) length
Withtimeout: (nstimeinterval) timeout
Buffer: (Nsmutabledata *) buffer
Bufferoffset: (Nsuinteger) offset
Tag: (long) tag;
/**
* * Entrust method
* * This method has been mentioned above
*/
-(void) Onsocket: (Asyncsocket *) sock didreaddata: (NSData *) _data Withtag: (long) tag.
Workaround:
Each time the client sends a request. You first receive only two bytes of length bytes, such as the following:
[Sendsocket readdatatolength:2 withTimeout:set.timeout Tag:tag]; [Sendsocket writedata:data withTimeout:set.timeout Tag:tag];
Then, when there are available bytes to reach the socket receive buffer, triggering the following method, we do in the following processing, such as to overcome the problem of sticky packets, but also overcome the data too large. Receive the complete question 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 blog original article. Blogs, without consent, may not be reproduced.
Asyncsocket long connecting rod packaging problem Solving