如何使用Bonjour浏览所有服务,无论其服务类型如何?
NSNetServiceBrowser可以浏览特定的服务类型 - “_music._tcp”。代码示例:
NSNetServiceBrowser *serviceBrowser;
serviceBrowser = [[NSNetServiceBrowser alloc] init];
[serviceBrowser setDelegate:delegateObject];
[serviceBrowser searchForServicesOfType:@"_music._tcp" inDomain:@""];
使用此代码,我将获得具有“_music._tcp”服务类型的所有服务。为了创建服务,我正在使用Network Beacon app。
但我需要找到所有服务,无论他们的服务类型如何。 this post的答案 - 没有用。
当我使用时:
@"_services._dns-sd._udp."
而不是:
@"_music._tcp."
我一无所获。
我是初学iOS编程 - 我将非常感谢你的帮助。 我使用的代码book第18章。
我的代码: BonjourViewController.m
@implementation BonjourViewController
- (void)viewDidLoad
{
[self browseServices];
[super viewDidLoad];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.services count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//--display the name of each service---
cell.textLabel.text = [[self.services objectAtIndex:indexPath.row]name];
return cell;
}
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
[self.services addObject:aNetService];
_debug.text = [_debug.text stringByAppendingString:@"Found service. Resolving address ...\n"];
[self resolveIPAddress:aNetService];
}
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService: (NSNetService *)aNetService moreComing:(BOOL)moreComing
{
[self.services removeObject:aNetService];
_debug.text = [_debug.text stringByAppendingFormat:@"Removed: %@\n", [aNetService hostName]];
[self.tbView reloadData];
}
-(void)resolveIPAddress:(NSNetService *)service
{
NSNetService *remoteService = service;
remoteService.delegate = self;
[remoteService resolveWithTimeout:10];
}
-(void)netServiceDidResolveAddress:(NSNetService *)sender
{
NSData *address = nil;
struct sockaddr_in *socketAddres = nil;
NSString *ipString = nil;
int port;
for (int i=0; i<[[sender addresses] count];i++)
{
address = [[sender addresses] objectAtIndex: i];
socketAddress = (struct sockaddr_in *) [address bytes];
ipString = [NSString stringWithFormat:@"%s", inet_ntoa(socketAddres->sin_addr)];
port = socketAddres->sin_port;
_debug.text = [_debug.text stringByAppendingFormat:@"Resolved: %@-->%@:%u\n", [sender hostName], ipString, port];
}
[self.tbView reloadData];
}
-(void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
{
_debug.text = [_debug.text stringByAppendingFormat:@"Could not resolve: %@\n", errorDict];
}
-(void)browseServices
{
self.services = [[NSMutableArray alloc]init];
self.browser = [[NSNetServiceBrowser alloc]init];
self.browser.delegate = self;
[self.browser searchForServicesOfType:@"_music._tcp." inDomain:@""];
}