title: Cocoa Read NSInputStream from FTP connection
body: <p>I (apparently) manage to make a ftp connection, but fail to read anything from it, and with good cause: I don't reach the reading until the connection has timed out.</p>
  
  <p>Here's my code:</p>
  
  <p>header:</p>
  
  <pre><code>NSInputStream *iStream;
  NSOutputStream *oStream;
  </code></pre>
  
  <p>implementation:</p>
  
  <pre><code>        NSHost *host = [NSHost hostWithAddress:@"127.0.0.1"];
          [iStream open]; // SOLUTION changes
          [NSStream getStreamsToHost:host port:3333 inputStream:&amp;iStream outputStream:&amp;oStream];
  
          NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:1];
          [settings setObject:(NSString *)NSStreamSocketSecurityLevelTLSv1 forKey:(NSString *)kCFStreamSSLLevel];
          [settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];
          [settings setObject:@"127.0.0.1" forKey:(NSString *)kCFStreamSSLPeerName];
  
          /*[iStream retain];   
          [iStream setDelegate:self];
          [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
          CFReadStreamSetProperty((CFReadStreamRef)iStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
          [iStream setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];*/
          //[iStream open]; SOLUTION changes: moved up
  </code></pre>
  
  <p>For eventhandling I use this, based on <a href="http://www.cocoadev.com/index.pl?NSStream" rel="nofollow noreferrer">http://www.cocoadev.com/index.pl?NSStream</a>:</p>
  
  <pre><code>- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
  {
      if (aStream == iStream) {
          [self handleInputStreamEvent:eventCode];
      } else if (aStream == oStream) {
          [self handleOutputStreamEvent:eventCode];
      }
  }
  - (void)handleInputStreamEvent:(NSStreamEvent)eventCode
  {
      switch (eventCode) {
      // SOLUTION changes: new inputstream handler
      case NSStreamEventHasBytesAvailable:
          if(!_data) {
              _data = [[NSMutableData data] retain];
          }
          uint8_t buf[1024];
          unsigned int len = 0;
          len = [(NSInputStream *)iStream read:buf maxLength:1024];
          if(len) {
              [_data appendBytes:(const void *)buf length:len];
              [bytesRead setIntValue:[bytesRead intValue]+len];
          }
          [self readBytes];
          break;
      case NSStreamEventOpenCompleted:
          NSLog(@"NSStreamEventOpenCompleted");
          break;
      case NSStreamEventEndEncountered:
          NSLog(@"NSStreamEventEndEncountered");
          break;
      case NSStreamEventErrorOccurred:
          NSLog(@"An error occurred on the input stream.");
          break;
      }
  }
  </code></pre>
  
  <p>I've left out oStream setup from this post to keep it minimal.</p>
  
  <p>I have not sent a request to the FTP to switch to ssl yet.</p>
  
  <p>Any help is greatly appreciated as I find Xcode quite horrible for debugging (no exception or error msg on failed steps what so ever).</p>
  
  <ul>
  <li>Chuck</li>
  </ul>