Question

So I have both an input stream and an output stream and they are setup as the code provided below. The input stream's delegate method NSStreamEventHasBytesAvailable is called only when i write somedata onto the output stream. Why is that?

// connect to the server and bind the input/output streams
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, _serverAddr, _serverPort,
                                   &readStream, &writeStream);

_inputStream = (__bridge_transfer NSInputStream *)readStream;
_outputStream = (__bridge_transfer NSOutputStream *)writeStream;

// attach the streams to the current processor run loop
[_inputStream setDelegate:self];    

dispatch_async(_inputStreamQueue, ^{
    [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                            forMode:NSRunLoopCommonModes];

    [_inputStream open];
    [[NSRunLoop currentRunLoop] run];
});


dispatch_async(_outputStreamQueue, ^{
    [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                             forMode:NSRunLoopCommonModes];
    [_outputStream open];
    [[NSRunLoop currentRunLoop] run];
});

InputStreamDelegate

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
{
dispatch_async(dispatch_get_main_queue(), ^(void) {
    if (stream == _inputStream) {
        [self inputStreamHandleEvent:eventCode];
    }
});

}

 - (void)inputStreamHandleEvent:(NSStreamEvent)eventCode
{
    switch (eventCode) {
    case NSStreamEventHasSpaceAvailable:
    {
        break;
    }
    case NSStreamEventEndEncountered:
    {
        break;
    }

    case NSStreamEventNone:
    {
        break;
    }

    case NSStreamEventErrorOccurred:
    {
        NSLog(@"NSStreamEventErrorOccurred");
        NSError* error = [_inputStream streamError];
        NSString* errorMessage = [NSString stringWithFormat:@"%@ (Code = %ld)",
                                  [error localizedDescription],
                                  (long)[error code]];
        UIAlertView *wifiLostAlert = [[UIAlertView alloc]
                                      initWithTitle:@"Input Stream Error"
                                      message:errorMessage
                                      delegate:nil
                                      cancelButtonTitle:@"Continue"
                                      otherButtonTitles:nil];
        [wifiLostAlert show];

        break;
    }

    case NSStreamEventHasBytesAvailable:
    {
        uint8_t buf[1024];
        int read = 0;

        while ([_inputStream hasBytesAvailable])
        {
            read = [(NSInputStream *)_inputStream read : buf maxLength : 1024];
            if (read > 0)
            {
                NSLog(@"%d bytes read from the input stream.", read);
                [_recvBuf appendBytes:(const void *)buf length:read];
                int processedBytes = 0;
                do
                    {
                        processedBytes = [self processPacket];
                    }
                while (processedBytes > 0);
            }

            else
            {
                NSLog(@"End of the stream reached, or a socket error. Disconnecting.");
                [self disconnect];
            }
        }
        break;
    }

    case NSStreamEventOpenCompleted:
    {
        break;
    }
}

}

My problem:

So, for the inputStream, I read the received data through the callbacks. This does not happen all the time. When ever the server responds back, it's not reading the result. The input stream seems to work ONLY when I send some data through the output stream, and then it reads the input stream values for the previous communication.

In terms of logical sequence...

What I expect is this.

1 --> 1'

2 --> 2'

3 --> 3'

This is reality

1 -->

2 --> 1'

3 --> 2'

Was it helpful?

Solution

Turns out it was a server-side issue. Code is good.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top