套接字断开时调用哪种方法

时间:2012-12-11 05:49:24

标签: iphone ios nsstream

在NSStreamDelegate中自动断开套接字时,有人知道调用哪个方法吗?因为当socket断开连接时,它将再次调用- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent方法,因此会产生很多问题。谁能告诉我如何处理插座断开连接?

1 个答案:

答案 0 :(得分:0)

被调用的方法又是- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent。由您来检测发生了哪种事件,并以适当的方式做出反应。

例如,请考虑Apple的文档中的以下示例:

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)streamEvent {
    switch(streamEvent) {
      case NSStreamEventHasBytesAvailable:;
      // whatever...
      break;
      case NSStreamEventEndEncountered:;
        [self processIncomingBytes];
        if (stream == ostream) {
            // When the output stream is closed, no more writing will succeed and
            // will abandon the processing of any pending requests and further
            // incoming bytes.
           [self invalidate];
        }
        break;
      case NSStreamEventErrorOccurred:;
        NSLog(@"HTTPServer stream error: %@", [stream streamError]);
        break;
      default:
        break;
    }
}