title: How to get read/write streams after dns_sd DNSServiceResolve in iOS
body: <p>The goal is to get read/write streams after a service is successfully resolved by with the dns_sd API. I started with Apple's DNSSDObjects sample project and I'm editing the DNSSDService.m file to get read and write streams after the service is resolved. </p>
  
  <p>Here's what I've got so far. It seems like it should work, but it does't :(</p>
  
  <p>I got this far by following the code on <a href="https://stackoverflow.com/questions/806116/is-there-anything-like-getstreamstohost-on-real-iphone-device">this thread</a>, though I'm not entirely sure that it's how this should be done.</p>
  
  <p>EDIT: Apple's documentation <a href="http://developer.apple.com/library/ios/#qa/qa1753/_index.html#//apple_ref/doc/uid/DTS40011315" rel="nofollow noreferrer">here</a> confirms that this is how it should be done..."So, once you've resolved the service using DNSServiceResolve, you should pass the service's DNS name (the hosttarget parameter to your DNSServiceResolveReply callback) to a connect-by-name API (like CFStreamCreatePairWithSocketToHost)."</p>
  
  <pre><code>// Called by DNS-SD when something happens with the resolve operation.
  static void ResolveReplyCallback(
      DNSServiceRef           sdRef,
      DNSServiceFlags         flags,
      uint32_t                interfaceIndex,
      DNSServiceErrorType     errorCode,
      const char *            fullname,
      const char *            hosttarget,
      uint16_t                port,
      uint16_t                txtLen,
      const unsigned char *   txtRecord,
      void *                  context
  ) 
  {
    CFStringRef host = CFStringCreateWithCString(kCFAllocatorDefault,
                                                 hosttarget, 
                                                 kCFStringEncodingUTF8);  
      DNSSDService *       obj;
      #pragma unused(interfaceIndex)
  
      assert([NSThread isMainThread]);  // b/c sdRef dispatches to the main queue
  
      obj = (__bridge DNSSDService *) context;
      assert([obj isKindOfClass:[DNSSDService class]]);
      assert(sdRef == obj-&gt;sdRef_);
      #pragma unused(sdRef)
      #pragma unused(flags)
      #pragma unused(fullname)
      #pragma unused(txtLen)
      #pragma unused(txtRecord)
  
      if (errorCode == kDNSServiceErr_NoError) {
          [obj resolveReplyWithTarget:[NSString stringWithUTF8String:hosttarget] 
                                 port:ntohs(port)];
      } else {
        [obj stopWithError:[NSError errorWithDomain:NSNetServicesErrorDomain
                                               code:errorCode
                                           userInfo:nil]
                    notify:YES];
      }
  
  //now let's get read&amp;write streams?
    CFReadStreamRef   readStream = NULL;
    CFWriteStreamRef  writeStream = NULL;
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
                                       host,
                                       port,
                                       &amp;readStream,
                                       &amp;writeStream
                                       );
    if (readStream &amp;&amp; writeStream) {
      CFReadStreamSetProperty(readStream,
                              kCFStreamPropertyShouldCloseNativeSocket, 
                              kCFBooleanTrue);
      CFWriteStreamSetProperty(writeStream,
                              kCFStreamPropertyShouldCloseNativeSocket, 
                              kCFBooleanTrue);
  
      obj.inputStream =  (__bridge_transfer  NSInputStream *) readStream;
      obj.outputStream = (__bridge_transfer NSOutputStream *) writeStream;
  
    }
    CFRelease(host);
  }
  </code></pre>