Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just announce the service, just like <a href="https://stackoverflow.com/q/3845920/39974">tc. has said below</a>:</p> <pre><code>self.netService = [[[NSNetService alloc] initWithDomain:@"" type:@"_http._tcp" name:@"" port:8080] autorelease]; [self.netService publish]; </code></pre> <p>With iOS5, however, let's-call-it "Bluetooth Bonjour" is disabled by default, so you have to use the C API declared in <code>&lt;dns_sd.h&gt;</code>.</p> <pre><code>DNSServiceRef serviceRef; DNSServiceRegister(&amp;serviceRef, // sdRef kDNSServiceFlagsIncludeP2P, // interfaceIndex 0, // flags NULL, // name "_http._tcp", // regtype NULL, // domain NULL, // host 1291, // port 0, // txtLen NULL, // txtRecord NULL, // callBack, NULL // context ); </code></pre> <p>This is just the announcement part; resolving is a bit more complex. I suggest you take a look at the following examples from Apple:</p> <ul> <li>SRVResolver - demonstrates how you can look up a service using API declared in <code>&lt;dns_sd.h&gt;</code>. Targets OS X, but includes a class called <code>SRVResolver</code> which you can use on iOS as easily as you can use it on OS X. For iOS 5 Bluetooth P2P to work, update the call to <code>DNSServiceQueryRecord()</code> to pass <code>kDNSServiceFlagsIncludeP2P</code> as the <code>interfaceIndex</code>. <i>(<b>NOTE!</b> This sample does not seem to exist in OS X 10.8 docset. It can be found in 10.6 and 10.7 docsets. In 10.8, there's the DNSSDObjects example, but I didn't look exactly at what it does.)</i></li> <li>WiTap - as long as you don't actually care about Bluetooth support on iOS 5, just look at the example called WiTap, which demonstrates not only the beautiful Objective-C API, but also how you can create a server using CFSocket APIs (thin wrappers around BSD sockets). You'll want to look at this even if you are using SRVResolver to see how to use C-based API from <code>&lt;dns_sd.h&gt;</code>.</li> </ul> <p>After announcing or resolving your service, you use regular BSD sockets to listen or connect. When writing a server, you may even want to first <code>listen()</code> on port 0 (zero), and then query which random available port was assigned to you. After querying for that, announce this port instead of a fixed one. That's exactly what WiTap example is doing (but with CFSocket API instead of BSD socket API).</p> <p>For more info on BSD sockets, just Google around for a tutorial.</p> <p><em>Note: information about iOS 5 comes from Apple's <a href="http://developer.apple.com/library/ios/#qa/qa1753/_index.html#//apple_ref/doc/uid/DTS40011315" rel="nofollow noreferrer">Technical Q&amp;A QA1753</a>.</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload