开发者

NSNetservice did not get netServiceDidPublish delegate call

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.


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.


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜