title: Creating p2p connection iOS
body: <p>Okay. So I have been trying to connect devices p2p with streams for a week. Still no result and i am getting crazy and desperate. Please Don't send me to Ray Wenderlich tutorial and GCD wiki or to CFStream Guide as i have surfed it to the holes.</p>
  
  <p>So 1) Variant Here i</p>
  
  <ol>
  <li>Get Ip Of My device</li>
  <li>Manually input ip in text field of other device</li>
  <li>Init network communication on one device.</li>
  <li><p>Error - connecting to host.</p>
  
  <pre><code> + (NSString *)getIPAddress {
  
   NSString *address = @"error";
   struct ifaddrs *interfaces = NULL;
  struct ifaddrs *temp_addr = NULL;
  int success = 0;
  // retrieve the current interfaces - returns 0 on success
   success = getifaddrs(&amp;interfaces);
   if (success == 0) {
   // Loop through linked list of interfaces
   temp_addr = interfaces;
    while(temp_addr != NULL) {
      if(temp_addr-&gt;ifa_addr-&gt;sa_family == AF_INET) {
      // Check if interface is en0 which is the wifi connection on the iPhone
         if([[NSString stringWithUTF8String:temp_addr-&gt;ifa_name] isEqualToString:@"en0"]) {
              // Get NSString from C String
              address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr-&gt;ifa_addr)-&gt;sin_addr)];
  
          }
  
      }
  
      temp_addr = temp_addr-&gt;ifa_next;
  }
   }
   // Free memory
  freeifaddrs(interfaces);
  return address;
  
  }
  </code></pre>
  
  <ul>
  <li>(void) initNetworkCommunication:(NSString*)ipToConnect {
  NSString *urlStr = ipToConnect;
  if (![urlStr isEqualToString:@""]) {
  NSURL *website = [NSURL URLWithString:urlStr];
  if (!website) {
      NSLog(@"%@ is not a valid URL");
      return;
  }
  CFReadStreamRef readStream;
  CFWriteStreamRef writeStream;
  CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)[website host], 80, &amp;readStream, &amp;writeStream);</li>
  </ul>
  
  <p>NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
  NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
  [inputStream setDelegate:self];
  [outputStream setDelegate:self];
  [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  [inputStream open];
  [outputStream open];</p>
  
  <p>}</p>
  
  <ul>
  <li>(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {</li>
  </ul>
  
  <p>NSLog(@"stream event %lu", streamEvent);</p>
  
  <p>switch (streamEvent) {</p>
  
  <pre><code>case NSStreamEventOpenCompleted:
      NSLog(@"Stream opened");
      break;
  case NSStreamEventHasBytesAvailable:
  
      if (theStream == inputStream) {
  
          uint8_t buffer[1024];
          int len;
  
          while ([inputStream hasBytesAvailable]) {
              len = [inputStream read:buffer maxLength:sizeof(buffer)];
              if (len &gt; 0) {
  
                  NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
  
                  if (nil != output) {
  
                      NSLog(@"server said: %@", output);
                      //[self messageReceived:output];
  
                  }
              }
          }
      }
      break;
  
  
  case NSStreamEventErrorOccurred:
  
      NSLog(@"Can not connect to the host!");
      break;
  
  case NSStreamEventEndEncountered:
  
      [theStream close];
      [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
   //   [theStream release];
      theStream = nil;
  
      break;
  default:
      NSLog(@"Unknown event");
  </code></pre>
  
  <p>}</p>
  
  <p>}</p></li>
  </ol>
  
  <p>Variant 2 with GCD.
  1. Same
  2. Set both devices to listen to input</p>
  
  <pre><code>+(void)listenSocket:(GCDAsyncSocket*)listenSocket
  {
  
  listenSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
  
  NSError *error = nil;
  if (![listenSocket acceptOnPort:80 error:&amp;error])
  {
      NSLog(@"I goofed: %@", error);
  }
  
  }
  </code></pre>
  
  <p>3) Tried to connect with manually inputting IP</p>
  
  <pre><code> +(void)connectToDeviceWithIp:(NSString*)deviceIp andSend:(HSUserCard*)tempCard andSocket:(GCDAsyncSocket*)tempSocket
  {
  
  tempSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
  NSError *err = nil;
  if (![tempSocket connectToHost:deviceIp onPort:80 error:&amp;err])
      NSLog(@"I goofed: %@", err);
  
  
  
  [tempSocket writeData:[NSKeyedArchiver archivedDataWithRootObject:tempCard] withTimeout:-1 tag:1];
  }
  </code></pre>
  
  <p>4) Result nothing - i did put several breaks on every delegate function  =NOTHING.</p>
  
  <p>Omg - i solved this task on Android in 20 minutes! But here it just drives crazy. Tried in several networks. Thrue 3g, home wifi. Please someone help!</p>