質問

Can anybody know which method is called when socket is automatically disconneted in NSStreamDelegate ? Because when socket is disconnected then its will again called - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent method so it will create lots of problem. Can anybody tell me how to handle socket disconnection ?

役に立ちましたか?

解決

The method that is called is again - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent. It is up to you to detect which kind of event happened, and to react in a proper way.

E.g., consider the following example from Apple's documentation:

- (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;
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top