开发者

How to discover network devices with Cocoa Touch?

I want to be able t开发者_如何学编程o enumerate the names of devices on a local network from a device running iPhone OS 3.x (iPhone/iPad). I have tried using NSNetServiceBrowser to find all services like so:

[serviceBrowser searchForServicesOfType:@"_services._dns-sd._udp." inDomain:@"local."];

this returns results but when I try and resolve the addresses I get the following errors back

NSNetServicesErrorCode = -72004;
NSNetServicesErrorDomain = 10;

I have looked up the error and it appears there is a bad argument?

[kCFNetServiceErrorBadArgument
A required argument was not provided or was not valid.]

if I do a service specific search like [serviceBrowser searchForServicesOfType:@"_ipp._tcp." inDomain:@""]; resolutions works fine.

So, am I on the right track with NSNetServiceBrowser or is there some other method that will allow me to enumerate the names of devices connected to my network?


This is the correct approach. Potentially, the reason you have a NSNetServicesBadArgumentError is because your serviceType string @"_services._dns-sd._udp." is invalid try @"_services._dns-sd._udp" instead i.e. without the trailing period.

Apple's documentation is confusing on this point. In the NSNetServiceBrowser Class Reference it states that:

The serviceType argument must contain both the service type and transport layer information. To ensure that the mDNS responder searches for services, rather than hosts, make sure to prefix both the service name and transport layer name with an underscore character (“_”). For example, to search for an HTTP service on TCP, you would use the type string “_http._tcp.“. Note that the period character at the end is required.

However, in the NSNetServices and CFNetServices Programming Guide, the example for Initializing the Browser and Starting a Search clearly doesn't use a period at the end:

serviceBrowser = [[NSNetServiceBrowser alloc] init];
[serviceBrowser setDelegate:delegateObject];
[serviceBrowser searchForServicesOfType:@"_music._tcp" inDomain:@""];

Try it without & see if ou have any luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜