2

所以我有一个输入流和一个输出流,它们被设置为下面提供的代码。NSStreamEventHasBytesAvailable只有当我将一些数据写入输出流时,才会调用输入流的委托方法。这是为什么?

// 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];
});

输入流委托

- (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;
    }
}

}

我的问题:

因此,对于 inputStream,我通过回调读取接收到的数据。这不会一直发生。当服务器响应时,它不会读取结果。输入流似乎仅在我通过输出流发送一些数据时才起作用,然后它读取先前通信的输入流值。

就逻辑顺序而言...

我期待的是这个。

1 --> 1'

2 --> 2'

3 --> 3'

这是现实

1 -->

2 --> 1'

3 --> 2'

4

1 回答 1

0

原来这是一个服务器端问题。代码很好。

于 2014-03-03T06:21:40.570 回答