(iphone)如何强制关闭套接字流?

时间:2011-07-01 17:45:42

标签: iphone objective-c cocoa-touch

我使用以下代码连接到服务器并处理事件 几乎是http://www.devx.com/wireless/Article/43551

的复制粘贴

我想在NSStreamEventEndEncountered之前强制关闭流。
文档缺乏,并且很难弄清楚如何强制关闭与连接相关的流。 (如果必须的话,关闭底层插座)

谢谢

-(void) connectToServerUsingStream:(NSString *)urlStr
                            portNo: (uint) portNo {

    if (![urlStr isEqualToString:@""]) {
        NSURL *website = [NSURL URLWithString:urlStr];
        if (!website) {
            NSLog(@"%@ is not a valid URL");
            return;
        } else {
            [NSStream getStreamsToHostNamed:urlStr
                      port:portNo
                      inputStream:&iStream
                      outputStream:&oStream];
            [iStream retain];
            [oStream retain];

            [iStream setDelegate:self];
            [oStream setDelegate:self];

            [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                     forMode:NSDefaultRunLoopMode];
            [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                     forMode:NSDefaultRunLoopMode];

            [oStream open];
            [iStream open];
        }
    }
}

并处理事件

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

    NSLog(@"stream event %d", eventCode) ;
    if( stream == iStream ) NSLog(@"on input stream");
    else if( stream == oStream ) NSLog(@"on output stream");
    else NSLog(@"on unknown stream identifier") ;

    switch(eventCode) {
        case NSStreamEventEndEncountered:
            {
                NSLog(@"stream ended; will be closed") ;
                [stream close];
                [stream removeFromRunLoop:[NSRunLoop currentRunLoop]
                        forMode:NSDefaultRunLoopMode];
                [stream release];
                stream = nil; // stream is ivar, so reinit it                                                                                                                                                                                                                 

                break;
            }
        case NSStreamEventErrorOccurred:
            NSLog(@"stream error") ;
            break ;

        case NSStreamEventHasBytesAvailable:
            //TODO: read here                                                                                                                                                                                                                                                 
            break ;

        case NSStreamEventNone:
            NSLog(@"stream null event") ;
            break ;

        case NSStreamEventOpenCompleted:
            NSLog(@"stream is now open") ;
            break ;

        case NSStreamEventHasSpaceAvailable:
            //write here                                                                                                                                                                                                                                                      
            break ;
    }

}

1 个答案:

答案 0 :(得分:1)

在计划流的线程/ runloop上执行以下方法:

    - (void)closeStream:(NSStream *)stream {
          [stream close];
          [stream removeFromRunLoop:[NSRunLoop currentRunLoop]
                  forMode:NSDefaultRunLoopMode];
          [stream release];
          stream = nil;
     }