0

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

4

1 回答 1

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;
    }
}
于 2012-12-11T07:18:14.563 回答