So, looking at your NSStreamDelegate , it looks like you have not implemented all the cases for this switch statement. I recently wrote an IRC client for OS X that uses NSStream and NSStreamDelegate very similar way, and I'm sure the compiler should complain if you haven't checked all cases there.
Looking back at part of my code , it looks like you should check cases
NSStreamEventHasSpaceAvailableNSStreamEventOpenCompletedNSStreamEventHasBytesAvailableNSStreamEventEndEncounteredNSStreamEventErrorOccurred
So, the case that you did not check is NSStreamEventHasSpaceAvailable , that is, when you can start recording to your stream.
edit: after reading my code again, I see in your sendMessage action that you use the outputStream object instead of writing the delegate, and then do the work yourself to read from the inputStream . I think you probably want to use a delegate and never read directly from your input stream, because it will greatly simplify how your code receives data from the network. From what I understand, NSStream should provide a small layer of abstraction around the fact that data is buffered from the network, so you don't need to do things like call usleep , while your input stream does not have bytes available for reading.
edit2: I read your update that your code never passed by while (![inputStream hasBytesAvailable]) , and it seems pretty obvious that the problem is that you are not using your threads correctly. The way I see it, the best way to use NSStream is to respond to events using its handleEvent:(NSStreamEvent) event method and never directly tell it to write bytes or sleep until the number of bytes is available.
In the code I linked to you, I have readDelegate and writeDelegate that handle NSStreams , you can take a look at how I use my writeDelegate. I basically have an addCommand:(NSString *) command method that puts the string to write to the stream in the queue, and then when the stream delegate can write bytes ( NSStreamEventHasSpaceAvailable ), I write as many bytes as I can. Hope this helps!
tjarratt
source share