title: TCP-Based Client communication doesn't work in iOS 11?
body: <p>I have an IoT application that uses <a href="https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/UsingSocketsandSocketStreams.html" rel="noreferrer">Socket Programming</a> to communicate with the device. All good up until iOS 11 released. It doesn't communicate in iOS 11 and above but it works in earlier versions ( Up to 10). Here is the code.</p>
  
  <p><strong>Establishing Socket Connection</strong>         </p>
  
  <pre><code>(void)setUpSocketConnection {
      @try {
          CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) VeranoHost,VeranoPort , &amp;readStream, &amp;writeStream);
          [self open];
      }
      @catch (NSException *exception) {
          NSLog(@"Open Exception:%@", exception.reason);
      }   
  }
  </code></pre>
  
  <p><strong>Opening Streams</strong></p>
  
  <pre><code>(void)open {
      //NSLog(@"Opening streams.");
      _outputStream = (__bridge  NSOutputStream *)writeStream;
      _inputStream = (__bridge  NSInputStream *)readStream;
      [_outputStream setDelegate:self];
      [_inputStream setDelegate:self];
  
      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
      dispatch_async(queue, ^ {
              [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
              [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
              [[NSRunLoop currentRunLoop] run];
      });
  
      [_outputStream open];
      [_inputStream open];
  
      //[self disableNaglesAlgorithmForStream:_inputStream];
     // NSLog(@"Connected");
  //    self.timeOutTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
  //                                                         target:self
  //                                                       selector:@selector(timerTimeOutAction:)
  //                                                       userInfo:nil
  //                                                        repeats:NO];
  }
  </code></pre>
  
  <p><strong>Writing to Output Stream</strong></p>
  
  <pre><code>(void)writeData:(NSString *)message forSocketType:(SOCKETTTYPE)socketType {
      self.socketType = socketType;
      NSData *data = [[NSData alloc] initWithData:[message dataUsingEncoding:NSASCIIStringEncoding]];
      [_outputStream write:[data bytes] maxLength:[data length]];
      [_outputStream close];
  
  }
  </code></pre>
  
  <p><strong>Event Handler</strong></p>
  
  <pre><code>(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
     // NSLog(@"stream event %lu", (unsigned long)streamEvent);
      switch (streamEvent) {
          case NSStreamEventOpenCompleted:{
  
             // NSLog(@"NSStreamEventOpenCompleted :::: Stream opened and connected");
          }
              break;
          case NSStreamEventHasBytesAvailable:
              // NSLog(@"NSStreamEventHasBytesAvailable :::: Stream opened and connected");
              if (theStream == _inputStream) {
                  uint8_t buffer[1024];
                  NSInteger 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 NSStreamEventHasSpaceAvailable:
             // NSLog(@"NSStreamEventHasSpaceAvailable :::: Stream has space available now");
              break;
  
          case NSStreamEventErrorOccurred:{
             NSError *theError = [theStream streamError];
              NSLog(@"Error Description:%@",theError.localizedDescription);
              [self close];
              if(self.delegate &amp;&amp;[self.delegate respondsToSelector:@selector(socketHandlerItem:failureWithError:forSocketType:)]){
                  [self.delegate socketHandlerItem:self failureWithError:[theStream streamError] forSocketType:self.socketType];
              }
               //NSLog(@"NSStreamEventErrorOccurred :::: %@",[theStream streamError].localizedDescription);
          }
              break;
  
          case NSStreamEventEndEncountered:
              [theStream close];
              [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
              if(self.delegate &amp;&amp;[self.delegate respondsToSelector:@selector(socketHandlerItem:eventStopedWithstatus:forSocketType:)]){
                  [self.delegate socketHandlerItem:self eventStopedWithstatus:YES forSocketType:self.socketType];
              }
             // NSLog(@"NSStreamEventEndEncountered :::: close stream Disconnected");
              break;
          default:
             // NSLog(@"Unknown event");
              break;
      }
  }
  </code></pre>
  
  <blockquote>
    <p>So, when connecting to the socket it enters to the
    <code>NSStreamEventErrorOccurred</code> in the event handler and logs - <code>The operation couldn’t be completed. No route to host</code></p>
  </blockquote>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p><strong>Update Dec 19, 2017</strong></p>
  
  <ul>
  <li>Got a <a href="http://apple.co/2By9Lhl" rel="noreferrer">Reachability</a> wrapper that allows to discover host address and I can confirm that it returns the NetworkStatus <code>ReachableViaWiFi</code></li>
  <li>Socket communication works fine, I tried by creating a sample server socket, able to send and receive data.</li>
  </ul>
  
  <p><strong>Update Dec 20, 2017 (1)</strong></p>
  
  <blockquote>
    <p>IoT device Details - <code>USR-WIFI232-S</code> Low Power WiFi Module</p>
    
    <p>User Manual <a href="https://www.elecrow.com/download/USR-WIFI232%20Low%20Power%20WiFi%20Module%20User%20Manual%20V2.2.pdf" rel="noreferrer">Here</a></p>
  </blockquote>
  
  <p><strong>Update Dec 20, 2017 (2)</strong></p>
  
  <p>Eurekaa, but the solution can't be applied in real time. While setting a nearest range of static IP address in iOS 11 corresponding to IoT module's host address the communication works fine. but it doesn't work with the dynamically allocated IP address.</p>