title: How do I get didReadData within GCDAsyncSocket execute within the current RunLoop?
body: <p>I'm trying to get a simple example working with GCDAsyncSocket, and am discovering that I'm missing certain bits of understanding and hope you fine people can help explain this.</p>
  
  <p>I've setup the GCDAsyncSocket stuff below:</p>
  
  <pre><code>dispatch_queue_t mainQueue = dispatch_get_main_queue();
  asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
  
  NSString *host = @"192.168.169.132";
  uint16_t port = 2112;
  
  DDLogInfo(@"Connecting to \"%@\" on port %hu...", host, port);
  self.viewController.label.text = @"Connecting...";
  
  NSError *error = nil;
  if (![asyncSocket connectToHost:host onPort:port withTimeout:5.0 error:&amp;error])
  {
      DDLogError(@"Error connecting: %@", error);
      self.viewController.label.text = @"Oops";
  }
  else
  {
      DDLogVerbose(@"Connecting...");
  }
  
  
  - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
  {
      DDLogInfo(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);
      self.viewController.label.text = @"Connected";
  
      // We're just going to send a test string to the server.
  
      NSString *myStr = @"testing...123...\r\n";
      NSData *myData = [myStr dataUsingEncoding:NSUTF8StringEncoding];
  
      [asyncSocket writeData:myData withTimeout:5.0 tag:0];
  }
  </code></pre>
  
  <p>And can see my socket test server app receive the string </p>
  
  <blockquote>
    <p>"testing...123...\r\n"</p>
  </blockquote>
  
  <p>But when I then have my socket test server send a string back, I naively expected the <strong><em>didReadData</em></strong> delegate to execute</p>
  
  <pre><code>- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
  </code></pre>
  
  <p>Yet the cold hard reality forced me learn that until I call</p>
  
  <pre><code>[asyncSocket readDataWithTimeout:5.0 tag:0];
  </code></pre>
  
  <p>... the <strong><em>didReadData</em></strong> delegate will not get called.</p>
  
  <p>OK, that's fine.  I get it.</p>
  
  <p>Reading up on <a href="http://code.google.com/p/cocoaasyncsocket/wiki/Reference_AsyncSocket#RunLoop_Configuration" rel="noreferrer">the documentation</a> some more it clearly states that </p>
  
  <blockquote>
    <p>AsyncSocket is a RunLoop based TCP socket library.</p>
  </blockquote>
  
  <p>So now I'm looking at this <strong><em>RunLoop</em></strong> thing, which in my view is like the <a href="http://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows" rel="noreferrer">Message loop in Microsoft Windows</a>.  Being that iOS is an event/msg driven architecture (just like Win32), then the default main thread I'm currently in obviously has it's own msg loop to handle events.</p>
  
  <p>My confusion is now having iOS RunLoop seem like some separate entity to have to work with in getting GCDAsyncSocket to work properly.</p>
  
  <p>When it states that its default set of run loop mode is NSDefaultRunLoopMode, which is in the main thread.</p>
  
  <p>Confused yet?</p>
  
  <p>So under Win32 my comm event handling code would look like this:</p>
  
  <pre><code>while( sCOMport.hCOMport != INVALID_HANDLE_VALUE )  // ...while the COM port is open...
  {
      // Wait for an event to occur on the port.
      WaitCommEvent( sCOMport.hCOMport, &amp;dwCommStatus, NULL );
  </code></pre>
  
  <p>It would of course be in its own thread (haven't gotten there yet using GCDAsyncSocket), but that would be its own "RunLoop" in a way.</p>
  
  <p>How do I do the same using <strong>GCDAsyncSocket</strong> so that I'm not stuck in some polling loop filling the queue with <strong>[asyncSocket readDataWithTimeout]</strong> calls?</p>
  
  <p>I feel we need better examples in using this library.</p>