GCDAsyncSocket - read stream never terminates

I have been playing with GCDAsyncSocket (MRC / iOS5.1) for a while, especially with "large" files (5 - 10 mb). Unfortunately, sometimes a read stream never terminates (for example, it gets stuck) with only a few bytes at the end of the stream; didReadPartialDataOfLength: stops giving me information, and didReadData does not start at all.

Here are some of my code (for both write and read examples, a connection has been established between the host and client)

RECORD

 #define kHeadTag 0 #define kDataTag 1 typedef struct { NSUInteger size; } head_t; -(void)sendData:(NSData *)__data { _sendData = [__data retain]; head_t header; header.size = __data.length; [_socket writeData:[NSData dataWithBytes:&header length:sizeof(header)] withTimeout:-1 tag:kHeadTag]; } -(void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag { if (tag == kHeadTag) { [sock writeData:_sendData withTimeout:-1 tag:kDataTag]; [_sendData release]; _sendData = nil; } } 

READING

 -(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { switch (tag) { // ------------------------------------- HEAD case kHeadTag:{ head_t head; [data getBytes:&head length:sizeof(head)]; _readHead = head; NSLog(@"Received header (total size = %i bytes)", head.size); [sock readDataToLength:head.size withTimeout:-1 tag:kDataTag]; } break; // ------------------------------------- BODY case kDataTag: { NSLog(@"Data received with %i bytes", data.length); [sock readDataToLength:sizeof(head_t) withTimeout:-1 tag:kHeadTag]; } break; } }; -(void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag { if (tag == kDataTag) { NSLog(@"Data read with %i bytes", partialLength); } }; 

Hopefully this is enough code to see what I'm doing wrong, or maybe it's bad practice to write / read large chunks of data.

+4
source share
1 answer

I added [self.partnerSocket writeData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:0]; at the end of the sendData: method and it works great for me. You just need to add separator characters at the end of the data. I can transfer about 48 MB of file from one iDevice to another iDevice

0
source

Source: https://habr.com/ru/post/1439342/


All Articles