几天来,我尝试实现一个在我的网络中浏览 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的任何提示。使用NSNetServiceBrowser
i 对服务的初始化方式没有影响。
有谁知道它出了什么问题?