Cocoa Socket 编程 NSInputStream 读取返回 0

发布于 2024-11-28 16:08:54 字数 1995 浏览 7 评论 0原文

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

(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,我是否遗漏了什么?

提前致谢 ,

In my App, have setup the stream like this,

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

}

and event handled like below,

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


                }

              }
    }
}

The problem is, read or getBuffer always returns 0, Am i missing something?

Thanks in Advance ,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间不值得 2024-12-05 16:08:54

不确定您的情况下 getBuffer:lenght: 的问题是什么,但这就是应该使用 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.
}

Not sure what the problem is with getBuffer:lenght: in your case, but this is how read:maxLength: should be used:

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文