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) {
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.
source share