title: how transfer file in ios use TCP socket (stream-out-in)
body: <p><code>enter code here</code>I write app transfer data from two devices (ios) by TCP socket.
  I success with NSString, i try with an image file.</p>
  
  <pre><code>CODE SEND DATA :
  NSString* docsDir;
  NSArray*  dirPaths;
  
  dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  docsDir = [dirPaths objectAtIndex:0];
  filePath = [[NSBundle mainBundle] pathForResource:@"image1.png" ofType:@""];
  NSFileManager * filemgr = [NSFileManager defaultManager];
  if ([filemgr fileExistsAtPath:filePath] == YES)
  {
      NSLog(@"OKie");
  }
  
  UInt64 offset = 0;
  UInt32 chunkSize = 1024;
  
  NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
  NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
  NSFileManager *fm = [NSFileManager defaultManager];
  NSLog(@"%llu", [[fm attributesOfItemAtPath:[filePath stringByStandardizingPath] error:NULL] fileSize]);
  chunkSize = [[fm attributesOfItemAtPath:[filePath stringByStandardizingPath] error:NULL] fileSize];
  NSData *data = [handle readDataOfLength:chunkSize];
  
  NSLog(@"Start send");
  
  while ([data length] &gt; 0 ) {        
      NSLog(@"sending");        
  
      autoreleasePool = [[NSAutoreleasePool alloc] init];   
      const uint8_t *bytes = (const uint8_t*)[data bytes];
      if([_outStream write:bytes maxLength:sizeof(const uint8_t)] == -1)
      {
          break;
      }
      offset += [data length];
      [handle seekToFileOffset:offset];
      data = [handle readDataOfLength:chunkSize];
      NSLog(@"%d",[data length]);
  }
  
  NSLog(@"Send Finish.");
  
  [handle closeFile];
  [autoreleasePool release];
  
  CODE RECEIVE DATA 
  
  
  
   - (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
      {
          switch(eventCode) {
              case NSStreamEventOpenCompleted:
              {
                  [self destroyPicker];
  
                  [_server release];
                  _server = nil;
  
                  if (stream == _inStream)
                      _inReady = YES;
                  else
                      _outReady = YES;
  
                  if (_inReady &amp;&amp; _outReady) {
                      NSLog(@"Connected");
                  }
                  break;
              }
              case NSStreamEventHasBytesAvailable:
              {
                  NSString* docsDir;
                  NSArray*  dirPaths;
                  dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                   docsDir = [dirPaths objectAtIndex:0];
                  filePath = [docsDir stringByAppendingPathComponent:@"image12.png"];
                  if (stream == _inStream) {
                      NSLog(@"packet");
      //              uint8_t b;
                      uint8_t *Buffer;
                      NSUInteger length;
                      //uint8_t buf[1024];
      //                unsigned int len = 0;             
                      [(NSInputStream *)stream getBuffer:&amp;Buffer length:&amp;length];
      //                len = [_inStream read:&amp;b maxLength:sizeof(uint8_t)];
                      NSData * data = [NSData dataWithBytes:&amp;Buffer  length:6957];
  
                      NSLog(@"Entering Loop.");
  
                          if ([data length] &gt; 0) 
                          {
  
                              NSFileHandle *file;
                              file = [NSFileHandle fileHandleForWritingAtPath: filePath];
                                  //file = [NSFileHandle fileHandleForUpdatingAtPath: filePath];
  
                              if (file == nil)
                              {
                                       NSLog(@"Failed to open file");
                                      [data writeToFile: filePath atomically: YES];
                                  NSFileManager *fm = [NSFileManager defaultManager];
                                  NSLog(@"%llu", [[fm attributesOfItemAtPath:[filePath stringByStandardizingPath] error:NULL] fileSize]);
                              }
                              else
                              {
                                  [file seekToEndOfFile];                            
                                  [file writeData: data];                            
                                  [file closeFile];
                                  NSFileManager *fm = [NSFileManager defaultManager];
                                  NSLog(@"%llu", [[fm attributesOfItemAtPath:[filePath stringByStandardizingPath] error:NULL] fileSize]);
                              }    
                          }
      //                }
                      NSLog(@"Exited Loop.");              
  
                  }
                  break;
              }
              case NSStreamEventErrorOccurred:
              {
                  [self _showAlert:@"Error encountered on stream!"];          
                  break;
              }
  
              case NSStreamEventEndEncountered:
              {
  
                  UIAlertView *alertView;
  
  
                  //Notify all tap views
      //          for(view in array)
      //              [view touchUp:YES];
  
                  alertView = [[UIAlertView alloc] initWithTitle:@"Peer Disconnected!" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Continue", nil];
                  [alertView show];
                  [alertView release];
  
                  break;
              }
          }
      }
  </code></pre>
  
  <p>*note fileSize = 6957 (file image1.png).
      i check file image12.png has written and has size = 6957;(check server has sent and data go to client 6957 bytes).
      Problem i can't read it as image file. (not image).
  -link down project sent-receive NSString(chat): <a href="http://www.mediafire.com/?f80if0y2x3lcoea" rel="nofollow">http://www.mediafire.com/?f80if0y2x3lcoea</a>
  -link down project sent-receive an image (problem): <a href="http://www.mediafire.com/?d2l9fl63i176m2z" rel="nofollow">http://www.mediafire.com/?d2l9fl63i176m2z</a>
  (i edit from witap of dev.apple)
      i hope you can help me</p>