title: (iphone) how to force close socket stream?
body: <p>I use the following code to connect to server and handle events.<br>
  almost copy-paste of <a href="http://www.devx.com/wireless/Article/43551" rel="nofollow">http://www.devx.com/wireless/Article/43551</a></p>
  
  <p>I want to force close the stream before NSStreamEventEndEncountered.<br>
  Documentation is lacking and having hard time to figure out how to force close the streams associated with connection. (or close underlying socket if I have to)</p>
  
  <p>Thank you</p>
  
  <pre><code>-(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:&amp;iStream
                        outputStream:&amp;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];
          }
      }
  }
  </code></pre>
  
  <p>and handles event </p>
  
  <pre><code>- (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 ;
      }
  
  }
  </code></pre>