On my server based on a TCP socket, I send packets downstream, where the packets consist of a header indicating the number of bytes in the packet, followed by this number of bytes. For those familiar with Erlang, I just set the {package, 4} parameter. On the iOS side, I have a code that looks like this if I want to find out the stream size for this post:
[asyncSocket readDataToLength:4 withTimeout:-1 tag:HEADER_TAG]
This works fine, and the following delegate method callback is called:
onSocket:didReadData:withTag:
I believe the next logical step is to determine the size of the stream, and I do this with
UInt32 readLength;
[data getBytes:&readLength length:4];
readLength = ntohl(readLength);
After hard-coding a string of 12 bytes on the server side, readLength really reads 12 on the client, so for now, everything is fine. I continue with the following:
[sock readDataToLength:readLength withTimeout:1 tag:MESSAGE_TAG]
, onSocket:didReadData:withTag: . - , , - , , :
- (NSTimeInterval)onSocket:(AsyncSocket *)sock shouldTimeoutReadWithTag:(long)tag elapsed:(NSTimeInterval)elapsed bytesDone:(NSUInteger)length
16 , 4 12- .
, , CocoaAsyncSocket. , ?
** **
, , . , readDataToLength . :
[socket readDataWithTimeout:-1 tag:HEADER_TAG]
:
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
if (tag == HEADER_TAG) {
UInt32 readLength;
[data getBytes:&readLength length:4];
readLength = ntohl(readLength);
int offset = 4;
NSRange range = NSMakeRange(offset, readLength);
char buffer[readLength];
[data getBytes:&buffer range:range];
NSLog(@"buffer %s", buffer);
} else if (tag == MESSAGE_TAG) {
}
}
, , . , , Erlang {, 4}. , . , readDataToLength? , ?