2

我想做的是在我的 ipod touch 上启用一个简单的 bonjour 服务。在我发布我的自定义 bonjour 服务后,委托人没有收到“netServiceDidPublish:”调用。我还检查了“netService:(NSNetService *)sender didNotPublish:”中没有任何错误消息。以下是我的代码部分:

// AsyncSocket class comes from an awesome project: cocoa async socket.
// http://code.google.com/p/cocoaasyncsocket/
AsyncSocket* listenSocket;

listenSocket = [[AsyncSocket alloc] initWithDelegate:self];
NSError *error;
if (![listenSocket acceptOnPort:0 error:&error])
{
    NSLog(@"Error starting server: %@", error);
    return NO;
}

int port = [listenSocket localPort];

NSLog(@"Server started on port: %hu", port);
isRunning = YES;

// register itself to bonjour service.
netService = [[[NSNetService alloc] initWithDomain:@"local."
                                             type:@"_sampleservice._tcp" 
                                             name:@"myservice" 
                                             port:port] autorelease];

if (!netService)
{
    NSLog(@"Failed to enable net service");
    [listenSocket disconnect];
    return NO;
}

[netService setDelegate:self];
[netService scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//[netService publishWithOptions:NSNetServiceNoAutoRename];
[netService publish];

在此代码部分之后,我可以获得“netServiceWillPublish”委托调用,但没有“netServiceDidPublish”有人知道吗?提前致谢。

4

2 回答 2

6

我注意到两件事。-scheduleInRunLoop:forMode:首先,除非需要将其移至不同的运行循环(或模式),否则不应调用。默认情况下,它已经安排在当前的运行循环中。其次,您似乎正在自动释放服务,这意味着一旦您返回运行循环,它就会被释放并释放。您需要将其粘贴在 ivar 或财产中并抓住它。

于 2010-10-28T04:07:20.250 回答
2

没必要-scheduleInRunLoop:forMode。实际上,根据证明您的NSNetService课程的库堆栈,您将获得不同的行为,有些会失败。你还需要retain你的 NSNetService。

我通过调度NSNetServiceNSNetServiceBrowser在 runLoop 上学到了一些不同的行为:

  1. In mDNS on Mac OS X, being accessed from Foundation framework, there's no harm scheduling on the runloop (tested it on Mac OS X 10.5, 10.6, 10.7 and 10.8).
  2. If you are using GNUStep's libgnustep-base compiled in Avahi compatibility mode (./configure --with-zeroconf-api=avahi) it also works but in my case I've gotten some segmentation faults if using many NSNetService instances being created and released.
  3. If you are using GNUStep's libgnustep-base compiled in Apple's mDNS compatibility mode (./configure --with-zeroconf-api=mdns) it won't work. You will receive a -72003 error both for publishing a NSNetService (error will come in -netService:didNotPublish:) and for browsing with a NSNetServiceBrowser (error will come in -netServiceBrowser:didNotSearch:). Tested this scenario both with Avahi's mDNS compatibility code (libavahi-compat-libdnssd1) and using Apple's mDNS directly, without Avahi.
于 2013-08-09T13:26:19.223 回答