33

I have been working on a NSStreamDelegate, I have implemented call back, I have initialized the input and output stream ilke this...

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStringRef host = CFSTR("74.125.224.72");
    UInt32 port = 2270;

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &inputStream, &writeStream);

    if (writeStream && inputStream) {

        inputStream = (__bridge  NSInputStream *)readStream;
        [inputStream setDelegate:self];
        [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [inputStream open];

        outputStream = (__bridge  NSOutputStream *)writeStream;
        [outputStream setDelegate:self];
        [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [outputStream open];
     }

Even after opened both the stream callback(stream:(NSStream *)theStream handleEvent:) is not called with NSStreamEventOpenCompleted for both streams. Can anyone help me what am I doing wrong here. Or What is the possibilities NSStreamEventOpenCompleted won't be called, I have seen in documentation, if opening failed it will not call this, if so why opening of streams is failing. Any idea?

thanks for your help.

3

2 Answers 2

0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.