title: Sending data by FTP
body: <p>Im trying to upload a file and im having a few problems. I have an input stream from a local path:</p>
  
  <pre><code>self.stmIn = [NSInputStream inputStreamWithFileAtPath:localPath];
  </code></pre>
  
  <p>The variable localPath has this URL:</p>
  
  <pre><code>/Users/JBG/Library/Application Support/iPhone Simulator/6.0/Applications/734F4DC6-8683-42BB-AB0D-A5553BC22C55/Documents/100046-003.jpg
  </code></pre>
  
  <p>I can open the stream without any problem. The problem comes when i try to read it:</p>
  
  <pre><code>bytesRead = [self.stmIn read:self.buffer maxLength:kSendBufferSize];
  </code></pre>
  
  <p>The result is -1. I dont understand the problem. Any idea?</p>
  
  <p>Thanks and regards</p>
  
  <p>EDIT: the code is based(taken) on the Apple's SimpleFTPSample but i implemented it as a NSObject class beside of UIViewController</p>
  
  <p>EDIT: got this info using the streamError method: Error Domain=NSPOSIXErrorDomain Code=14 "The operation coudn't be completed. Bad address"</p>
  
  <p>EDIT: I add the code:</p>
  
  <pre><code>- (uint8_t *)buffer {
      return self-&gt;buffer;
  }
  
  - (BOOL)isSending {
      return (self.stmOut != nil);
  }
  
  - (void)startSend {
      BOOL success;
      NSURL *url;
  
      [[NetworkManager sharedInstance] didStartNetworkOperation];
  
      assert([[NSFileManager defaultManager] fileExistsAtPath:localPath]);
  
      assert(self.stmOut == nil);      // don't tap send twice in a row!
      assert(self.stmIn == nil);         // ditto
  
      url = [[NetworkManager sharedInstance] smartURLForString:ftpPath];
      success = (url != nil);
  
      if (success) {
          url = CFBridgingRelease(CFURLCreateCopyAppendingPathComponent(NULL, (CFURLRef) url, (CFStringRef) [localPath lastPathComponent], false));
          success = (url != nil);
      }
  
      if ( ! success) {
          NSLog(@"Invalid URL");
      } else {
  
          self.stmIn = [NSInputStream inputStreamWithFileAtPath:localPath];
          assert(self.stmIn != nil);
  
          [self.stmIn open];
  
          self.stmOut = CFBridgingRelease(CFWriteStreamCreateWithFTPURL(NULL, (CFURLRef) url));
          assert(self.stmOut != nil);
  
          success = [self.stmOut setProperty:ftpUser forKey:(id)kCFStreamPropertyFTPUserName];
          assert(success);
          success = [self.stmOut setProperty:ftpPwd forKey:(id)kCFStreamPropertyFTPPassword];
          assert(success);
  
          self.stmOut.delegate = self;
          [self.stmOut scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
          [self.stmOut open];
      }
  }
  
  - (void)stopSendWithStatus:(NSString *)statusString {
      if (self.stmOut != nil) {
          [self.stmOut removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
          self.stmOut.delegate = nil;
          [self.stmOut close];
          self.stmOut = nil;
      }
      if (self.stmIn != nil) {
          [self.stmIn close];
          self.stmIn = nil;
      }
      if (statusString == nil) {
          statusString = @"Archivo subido";
      }
      [[NetworkManager sharedInstance] didStopNetworkOperation];
  }
  
  - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
      assert(aStream == self.stmOut);
  
      switch (eventCode) {
          case NSStreamEventOpenCompleted: {
              NSLog(@"Opened connection");
          } break;
          case NSStreamEventHasBytesAvailable: {
              assert(NO); 
          } break;
          case NSStreamEventHasSpaceAvailable: {
              NSLog(@"Sending");
              if (self.bufferOffset == self.bufferLimit) {
                  NSInteger   bytesRead;
  
                  bytesRead = [self.stmIn read:self.buffer maxLength:kSendBufferSize];
  
                  if (bytesRead == -1) {
                      [self stopSendWithStatus:@"File read error"];
                  } else if (bytesRead == 0) {
                      [self stopSendWithStatus:nil];
                  } else {
                      self.bufferOffset = 0;
                      self.bufferLimit  = bytesRead;
                  }
              }
  
              if (self.bufferOffset != self.bufferLimit) {
                  NSInteger   bytesWritten;
                  bytesWritten = [self.stmOut write:&amp;self.buffer[self.bufferOffset] maxLength:self.bufferLimit - self.bufferOffset];
                  assert(bytesWritten != 0);
                  if (bytesWritten == -1) {
                      [self stopSendWithStatus:@"Network write error"];
                  } else {
                      self.bufferOffset += bytesWritten;
                  }
              }
          } break;
          case NSStreamEventErrorOccurred: {
              [self stopSendWithStatus:@"Stream open error"];
          } break;
          case NSStreamEventEndEncountered: {
          // ignore
          } break;
          default: {
              assert(NO);
          } break;
      }
  }
  </code></pre>