3

无论服务类型如何,如何使用 Bonjour 浏览所有服务?

NSNetServiceBrowser 可以浏览特定的服务类型 - “_music._tcp”。代码示例:

NSNetServiceBrowser *serviceBrowser;

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

使用此代码,我可以获得所有具有“_music._tcp”服务类型的服务。为了创建服务,我正在使用Network Beacon app

但无论服务类型如何,我都需要找到所有服务。这篇文章的答案- 没有帮助。

当我使用时:

@"_services._dns-sd._udp."

代替:

@"_music._tcp."

我什么都得不到。

我是初学者 iOS 编程 - 我将非常感谢您的帮助。我使用的代码来自本书第 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:@""];
} 
4

0 回答 0