title: How to use NSOutputStream's write message?
body: <p>I want to send a UIImage to a server through a socket.</p>
  
  <p>a)I open the outputstream:</p>
  
  <pre><code>
  - (IBAction)send:(id)sender {
      NSURL *website = [NSURL URLWithString:str_IP];
      NSHost *host = [NSHost hostWithName:[website host]];
      [NSStream getStreamsToHost:host port:1100 inputStream:nil outputStream:&oStream];
      [oStream retain];
      [oStream setDelegate:self];
      [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
      [oStream open];
  }
  </code></pre>
  
  <p>b) I write NSData to outputstream after open completed and handle the error if error occurs.</p>
  
  <pre><code>
  - (void) stream: (NSStream *) stream handleEvent: (NSStreamEvent) eventCode
  {
      //printf("EVENT: Start.\n");
  
      switch(eventCode)
      {
              case NSStreamEventOpenCompleted:
              {
                  //printf("EVENT: Open completed.\n");
  
                  if(stream == oStream)
                      {
                          //printf("Sending...\n");
                          NSData *data = UIImageJPEGRepresentation(drawImage.image, 90);
                          NSInteger x = [oStream write:[data bytes] maxLength:[data length]];
                      }
  
                  break;
              }
              case NSStreamEventEndEncountered:
              {
                  //printf("EVENT: End encountered.\n");
                  break;
              }
              case NSStreamEventHasSpaceAvailable:
              {
                  //printf("EVENT: Has space available.\n");
                  break;
              }
              case NSStreamEventHasBytesAvailable:
              {
                  //printf("EVENT: Has bytes available.\n");
                  break;
              }
              case NSStreamEventErrorOccurred:
              {
                  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Occurred"       
                                                                  message:nil 
                                                                 delegate:nil             
                                                        cancelButtonTitle:@"OK"               
                                                        otherButtonTitles:nil];                   
                  [alert show];
                  [alert release];
                  break;
              }
              case NSStreamEventNone:
              {
                  //printf("EVENT: None.\n");
                  break;
              }
      }
  
      //printf("EVENT: End.\n");
  }
  
  </code></pre>
  
  <p>When I run this code, NSStreamEventOpenCompleted and NSStreamEventErrorOccurred is called.The NSOutputStream's write method was called successfully and all the data is not nil. But after the data is written to the oStream, the eventCode will change to NSStreamEventErrorOccurred. </p>
  
  <p>So I think maybe it's not the correct way to use [oStream write]. What's the correct way to use this message then? I find this message returns an NSInteger of -1073748088, what might be the problem?</p>