几天之内,我尝试实现一个应用程序,该程序可在我的网络中浏览Bonjour-Services并将其显示在控制台中(第一步)。我用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 的任何提示。使用NSNetServiceBrowser
对服务的初始化方式没有影响。
有人知道它出了什么问题吗?
答案 0 :(得分:0)
所以这里的问题是,当您搜索_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")