NSNetservice 未获得 netServiceDidPublish 委托调用
我想要做的是在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我注意到两件事。首先,您不应该调用 -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.无需
-scheduleInRunLoop:forMode
。实际上,根据证明您的 NSNetService 类的库堆栈,您将获得不同的行为,有些会失败。您还需要保留
您的 NSNetService。我通过在 runLoop 上调度
NSNetService
或NSNetServiceBrowser
学到了一些不同的行为:Foundation
框架访问,对 runloop 进行调度没有什么坏处(在 Mac OS X 10.5、10.6、10.7 和 10.8 上进行了测试)。libgnustep-base
(./configure --with-zeroconf-api=avahi
),它也可以工作,但就我而言,我'如果使用许多创建和发布的 NSNetService 实例,则会出现一些分段错误。./configure --with-zeroconf-api=mdns
) 下编译的 GNUStep 的libgnustep-base
,它将无法工作。发布 NSNetService 时(错误将出现在-netService:didNotPublish:
中)和使用 NSNetServiceBrowser 浏览(错误将出现在中),您都会收到
)。使用 Avahi 的 mDNS 兼容性代码 (-72003
错误>-netServiceBrowser:didNotSearch:libavahi-compat-libdnssd1
) 和直接使用 Apple 的 mDNS(不使用 Avahi)测试了此场景。No need to
-scheduleInRunLoop:forMode
. Actually, depending on the library stack that is proving yourNSNetService
class you will get different behaviors, some will fail. Also you need toretain
your NSNetService.Some different behaviors I've learned over scheduling the
NSNetService
orNSNetServiceBrowser
on the runLoop: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).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 manyNSNetService
instances being created and released.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.