【问题标题】:NSNetservice did not get netServiceDidPublish delegate callNSNetservice 没有得到 netServiceDidPublish 委托调用
【发布时间】:2010-10-21 06:12:16
【问题描述】:

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

【问题讨论】:

    标签: objective-c bonjour nsnetservice


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      我在 runLoop 上安排 NSNetServiceNSNetServiceBrowser 时学到了一些不同的行为:

      1. 在 Mac OS X 上的 mDNS 中,通过 Foundation 框架访问,运行循环上的调度没有任何危害(在 Mac OS X 10.5、10.6、10.7 和 10.8 上进行了测试)。
      2. 如果您使用在 Avahi 兼容模式 (./configure --with-zeroconf-api=avahi) 下编译的 GNUStep 的 libgnustep-base,它也可以工作,但在我的情况下,如果创建和发布许多 NSNetService 实例,我会遇到一些分段错误。
      3. 如果您使用在 Apple 的 mDNS 兼容模式 (./configure --with-zeroconf-api=mdns) 下编译的 GNUStep 的 libgnustep-base,它将无法工作。您将收到一个 -72003 错误,用于发布 NSNetService(错误将出现在 -netService:didNotPublish:)和使用 NSNetServiceBrowser 进行浏览(错误将出现在 -netServiceBrowser:didNotSearch:)。使用 Avahi 的 mDNS 兼容性代码 (libavahi-compat-libdnssd1) 和直接使用 Apple 的 mDNS 测试了这个场景,没有 Avahi。

      【讨论】:

        猜你喜欢
        • 2010-12-29
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 2016-11-08
        相关资源
        最近更新 更多