NSNetservice 未获得 netServiceDidPublish 委托调用

发布于 2024-09-28 02:32:50 字数 1359 浏览 9 评论 0原文

我想要做的是在我的 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”有人有任何想法吗?提前致谢。

what I want to do is enable a simple bonjour service on my ipod touch.
And after I publish my custom bonjour service, the delegator did not get "netServiceDidPublish:" call. I also check there is not any error message from "netService:(NSNetService *)sender didNotPublish:". Below is my code section:

// 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];

After this code section, I can get "netServiceWillPublish" delegated call, but no "netServiceDidPublish" Does somebody have any idea? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情仇皆在手 2024-10-05 02:32:50

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

Two things I noticed. First, you shouldn't call -scheduleInRunLoop:forMode: unless you need to move it to a different runloop (or mode). It's already scheduled in the current run loop by default. Second, you appear to be autoreleasing the service, which means it'll get released and dealloc'd as soon as you return to the runloop. You need to stick it in an ivar or property and hold on to it.

樱娆 2024-10-05 02:32:50

无需-scheduleInRunLoop:forMode。实际上,根据证明您的 NSNetService 类的库堆栈,您将获得不同的行为,有些会失败。您还需要保留您的 NSNetService。

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

  1. 在 Mac OS X 上的 mDNS 中,从 Foundation 框架访问,对 runloop 进行调度没有什么坏处(在 Mac OS X 10.5、10.6、10.7 和 10.8 上进行了测试)。
  2. 如果您使用的是在 Avahi 兼容模式下编译的 GNUStep 的 libgnustep-base (./configure --with-zeroconf-api=avahi),它也可以工作,但就我而言,我'如果使用许多创建和发布的 NSNetService 实例,则会出现一些分段错误。
  3. 如果您使用的是在 Apple 的 mDNS 兼容模式 (./configure --with-zeroconf-api=mdns) 下编译的 GNUStep 的 libgnustep-base,它将无法工作。发布 NSNetService 时(错误将出现在 -netService:didNotPublish: 中)和使用 NSNetServiceBrowser 浏览(错误将出现在 中),您都会收到 -72003 错误>-netServiceBrowser:didNotSearch:)。使用 Avahi 的 mDNS 兼容性代码 (libavahi-compat-libdnssd1) 和直接使用 Apple 的 mDNS(不使用 Avahi)测试了此场景。

No need to -scheduleInRunLoop:forMode. Actually, depending on the library stack that is proving your NSNetService class you will get different behaviors, some will fail. Also you need to retain your NSNetService.

Some different behaviors I've learned over scheduling the NSNetService or NSNetServiceBrowser on the 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文