How to callback NSStreamDelegate with NSStreamEventOpenCompleted? How to callback NSStreamDelegate with NSStreamEventOpenCompleted? ios ios

How to callback NSStreamDelegate with NSStreamEventOpenCompleted?


I use with very similar code and it works fine for me.Try the code below.

   NSString* host = @"192.168.2.105";    CFReadStreamRef readStream;    CFWriteStreamRef writeStream;    UInt32 port = 8008;    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)(host), port, &readStream, &writeStream);    if (writeStream && readStream) {        self.InputStream = (__bridge  NSInputStream *)readStream;        [self.InputStream setDelegate:self];        [self.InputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];        [self.InputStream open];        self.OutputStream = (__bridge  NSOutputStream *)writeStream;        [self.OutputStream setDelegate:self];        [self.OutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];        [self.OutputStream open];    }

If it doesn't work for you, I can to send you a small app that implement TCP Client and server for a example.


If it is running in a new NSThread, make sure the run loop of the thread is started after the stream setup, like CFRunLoopRun();