NSStream Delegate NSStreamEventHasBytesAvailable Not Being Called

4.5k Views Asked by At

I'm writing a messaging app that uses a NSStream to communicate with a server written in Python. The server works perfectly with a companion Python client. But when I connect to it with a NSStream, the NSInputStream doesn't seem to get any data. The NSOutputStream, however, works perfectly. I am opening the string like so:

-(void)openStream
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"tihmstar.dyndns.org", 80,       &readStream, &writeStream);

    inputStream = (__bridge_transfer NSInputStream *)readStream;
    outputStream = (__bridge_transfer NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];

    [self auth];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"InitCompleted" object:nil];
}

The delegate method is as so:

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
    {
        NSLog(@"Handle Event - ");
        switch (streamEvent)
        {
            case NSStreamEventOpenCompleted:
                NSLog(@"Stream opened");
                break;

            case NSStreamEventHasBytesAvailable:
                NSLog(@"Bytes Available!");
                if(theStream == inputStream)
                {
                    NSLog(@"inputStream is ready.");

                    uint8_t buf[1024];
                    unsigned int len = 0;

                    len = [inputStream read:buf maxLength:1024];

                    if(len > 0)
                    {
                        NSMutableData* data=[[NSMutableData alloc] initWithLength:0];

                        [data appendBytes: (const void *)buf length:len];

                        NSString* string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
                        NSLog(@"Server said- %@", string);
                        [self messageReceived:[string lowercaseString]];
                    }
                }
                break;

            case NSStreamEventErrorOccurred:
                NSLog(@"Can not connect to the host!");
                break;

            case NSStreamEventEndEncountered:
                NSLog(@"End Encountered");
                break;

            case NSStreamEventHasSpaceAvailable:
                NSLog(@"Space Availible.");
                break;

            default:
                NSLog(@"Unknown event- %u", streamEvent);
        }
    }

My problem is that case NSStreamEventHasBytesAvailable is never called, and therefore the message from the server is never being received. Anyone have any solutions for this? I've found some related questions here on StackOverflow, but none of them have been answered.

Thanks in advance.

2

There are 2 best solutions below

0
On

I was just looking into the code. try to remove delegate of NSInputStream. I was looking into the another Using NSXMLParser initWithStream: no parser delegate methods received

which deals with similar situation.

0
On

I don't know exactly the answer for your question but I know exactly that you shouldn't call

[self auth];

after NSStream open right away. You have to wait for

NSStreamEventHasSpaceAvailable

event for your NSOutputStream and write data inside it afterwards only.