There are two ways to get data from a stream: polling and using stream events.
Polling is simpler, but will block the thread in which it is running. If you use this method, you do not need to make calls to setDelegate: or scheduleInRunLoop:forMode: Polling is done by calling read:maxLength: .
NSInteger result; uint8_t buffer[BUFFER_LEN];
Using thread events requires installing a delegate and adding a thread to the run loop. Instead of blocking the stream, the stream sends a stream:handleEvent: message to its delegate when certain events occur, including when data is received. The delegate can then retrieve the data from the stream. The following is an example of the stream:handleEvent: method:
- (void)stream:(NSInputStream *)iStream handleEvent:(NSStreamEvent)event { BOOL shouldClose = NO; switch(event) { case NSStreamEventEndEncountered: shouldClose = YES;
ughoavgfhw
source share