Cocoa套接字编程NSInputStream读取返回0

Cocoa套接字编程NSInputStream读取返回0,cocoa,sockets,objective-c++,nsstream,Cocoa,Sockets,Objective C++,Nsstream,在我的应用程序中,像这样设置流 (void)connectStream:(NSString *)pHostName PortNo:(int)inPortNo HasInput:(bool)bInput HasOutput:(bool)bOutput{ NSHost *host = [NSHost hostWithName:pHostName]; //host = [NSHost hostWithAddress:pHostName]; [NSStream get

在我的应用程序中,像这样设置流

(void)connectStream:(NSString *)pHostName PortNo:(int)inPortNo HasInput:(bool)bInput HasOutput:(bool)bOutput{




    NSHost *host = [NSHost hostWithName:pHostName];

    //host = [NSHost hostWithAddress:pHostName];

    [NSStream getStreamsToHost:host port:inPortNo inputStream:&pInputStream
                  outputStream:&pOutputStream];

    [pInputStream retain];  

    [pOutputStream retain];

    [pInputStream setDelegate:self];

    [pOutputStream setDelegate:self];



    bool bUseSSL = YES;
    if (bUseSSL)
    { 

        [self setInputStreamSecurity];
        [self setOutputStreamSecurity];
    }


    [pOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                       forMode:NSDefaultRunLoopMode];

    [pInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                            forMode:NSDefaultRunLoopMode];


    [pInputStream open];

    [pOutputStream open];

}
事件处理方式如下所示

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent{

       switch(streamEvent){
        case NSStreamEventHasBytesAvailable:{
        if([theStream hasBytesAvailable]){
                unsigned int len=0;

                NSUInteger intLen;
                [theStream getBuffer:&pInputBuffer length:&intLen];
                [theStream read:pInputBuffer maxLength:MAX_INPUT_BUFF_LEN];

                if(intLen){         
                  NSMutableData *data=[[NSMutableData alloc] init];
                  [data appendBytes:pInputBuffer length:len];

                  [WebSocketEventData postGotBytesEvent:data Len:len];
                 }else{
                   NSError *theError = [theStream streamError];
                   NSString *pString = [theError localizedDescription];
                   int errorCode = [theError code];


                }

              }
    }
}
问题是,read或getBuffer总是返回0,我是否遗漏了什么


提前感谢,

不确定在您的情况下,getBuffer:Length:的问题是什么,但这就是应该如何使用
read:maxLength:

NSInteger bytesRead;

bytesRead = [theStream read:pInputBuffer maxLength:MAX_INPUT_BUFF_LEN];
if (bytesRead > 0) {
    // Handle input.
} else if (bytesRead == 0) {
    // Handle EOF.
} else {
    // Handle error.
}

谢谢,我也试过了,但没有改进,它总是返回0并立即关闭。你用Wireshark做过网络跟踪吗?流是否真的包含数据,或者流是否真的被对等方关闭了?谢谢,现在这样做,刚刚浏览了一些博客,似乎服务器可能已经关闭了套接字,所以流中没有任何内容。。。但这条赛道真的很有帮助。