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

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

}

私の問題:

そのため、inputStream については、コールバックを介して受信データを読み取ります。これは常に発生するわけではありません。サーバーが応答するたびに、結果を読み取っていません。入力ストリームは、出力ストリームを介してデータを送信した場合にのみ機能するようで、前の通信の入力ストリーム値を読み取ります。

論理的な順序で言えば...

私が期待するのはこれです。

1 --> 1'

2 --> 2'

3 --> 3'

これは現実です

1 -->

2 --> 1'

3 --> 2'

4

1 に答える 1

0

サーバー側の問題であることが判明しました。コードは良いです。

于 2014-03-03T06:21:40.570 に答える