0

几天来,我尝试实现一个在我的网络中浏览 Bonjour-Services 并在控制台中显示它们的应用程序(第一步)。我用 a 实现了一个类,NSNetServiceBrowser并按照这里的许多帖子中的说明对其进行了初始化。但是在找到服务后,我无法解决它以使我可以访问主机名。服务解析总是导致错误

-72004(NSNetServicesBadArgumentError)。

我尝试使用@“local”。作为域或我的特定服务来查找而不是@"_services._dns-sd._udp." 但这一切都会导致相同的结果。

//
//  Bonjour.m
//  BonjourTest 
//
//  Created by PellePe on 21.07.18.
//  Copyright © 2018 PellePe. All rights reserved.
//

#import "Bonjour.h"
#import <Foundation/Foundation.h>

#include <netinet/in.h>


#define SERVICE_DOMAIN @"local"
#define SERVICE_TYPE   @"_bonjourTest._tcp"
#define SERVICE_NAME   @"Bonjour Test"

@interface Bonjour () <NSNetServiceDelegate, NSNetServiceBrowserDelegate>

@property (nonatomic, strong) NSMutableArray<NSNetService *> *detectedServices;
@property (nonatomic, strong) NSNetServiceBrowser *netServiceBrowser;

@end


@implementation Bonjour

#pragma mark -
#pragma mark Service Browsing

- (BOOL)isBrowsing {
    return self.netServiceBrowser != NULL;
}

- (void)startServiceBrowsing:(NSString *)type {
    if (self.detectedServices == NULL) {
        self.detectedServices = [[NSMutableArray<NSNetService*> alloc] init];
    } else {
        [self.detectedServices removeAllObjects];
    }

    self.netServiceBrowser = [[NSNetServiceBrowser alloc] init];
    self.netServiceBrowser.includesPeerToPeer = YES;
    self.netServiceBrowser.delegate = self;

    [self.netServiceBrowser searchForServicesOfType: type  // SERVICE_TYPE
                                       inDomain: @""];
}

- (void)stopServiceBrowsing {
    assert(self.netServiceBrowser != NULL);

    [self.netServiceBrowser stop];
    self.netServiceBrowser = NULL;
}

#pragma mark -
#pragma NSNetServiceDelegate

-(void)netServiceWillResolve:(NSNetService *)sender {
    NSLog(@"netService will resolve - %@", sender.hostName);
}

- (void)netService:(NSNetService *)sender
     didNotResolve:(NSDictionary<NSString *,NSNumber *> *)errorDict {
    NSLog(@"netService did not resolve");

    for (NSString *key in [errorDict allKeys]) {
        NSLog(@"%@: %@", key, [errorDict objectForKey: key]);
    }
}

- (void)netServiceDidResolveAddress:(NSNetService *)sender {
    NSLog(@"netService did resolve address - %@", sender.hostName);  
}

#pragma mark -
#pragma mark NSNetServiceBrowserDelegate

- (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)browser {
    NSLog(@"Browser will search");
}

- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)browser {
    NSLog(@"Browser did stop search");
}

- (void)netServiceBrowser:(NSNetServiceBrowser *)browser     didFindService:(NSNetService *)service moreComing:(BOOL)moreComing {
    [self.detectedServices addObject: service];

    service.delegate = self;
    [service resolveWithTimeout: 100];

    NSLog(@"Find service: %@ - %@ - %@ (%@)", service.name, service.hostName, service.type, moreComing ? @"YES" : @"No");
}

- (void)netServiceBrowser:(NSNetServiceBrowser *)browser didRemoveService:(NSNetService *)service moreComing:(BOOL)moreComing {
    NSLog(@"Remove service: %@ - %@ - %@", service.name, service.hostName, service.type);
}

@end

我在网上搜索了好几个小时,但找不到关于错误 -72004的任何提示。使用NSNetServiceBrowseri 对服务的初始化方式没有影响。

有谁知道它出了什么问题?

4

1 回答 1

1

所以这里的问题是,当你搜索时,_services._dns-sd._udp.你不会得到实际的服务,但你会得到所有的服务类型,例如。

let browser = NetServiceBrowser()
browser.searchForServices(ofType: "_googlecast._tcp", inDomain: "local")

此查询将返回如下所示的服务

NetService(domain: "local", type: "_googlecast._tcp", name: "RoomTV")

这是有效的,您可以毫无问题地解决,但是当您像这样浏览时:

browser.searchForServices(ofType: "_services._dns-sd._udp.", inDomain: "local")

返回的服务将如下所示:

NetService(domain: ".", type: "_tcp.local.", name: "_googlecast")

因此,要解决此问题,您需要使用新类型再次搜索以获取实际服务。

let type = "\(service.name).\(service.type)"
               .replacingOccurrences(of: "local.", with: "")
browser.searchForServices(ofType: type, inDomain: "local")

于 2019-07-16T01:14:52.770 回答