views:

12

answers:

1

Hi, 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];

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.

A: 

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.

Kevin Ballard
It did work. Thank you so much.
dokinkon
Feel like accepting my answer then?
Kevin Ballard