Linuxios
Linuxios

Reputation: 35783

NSNetServiceBrowser not finding services that exist

I have a custom Cocoa (OSX) server and client, and the server publishes a bonjour service. I have the client searching for the server using NSNetServiceBrowser, but I never get any results. I know that the service is published because it is in the output of mdns -B _myservicetype._tcp. My searching and publishing code both search and publish with the same domain, name, and type, and are almost identical to the Apple sample code. Here's the publishing code:

service = [[NSNetService alloc] initWithDomain:@"" type:@"_scijs._tcp" name:@"ScienceJeopardyServer" port:4567];
[service publish];

And here's the search and delegate methods:

[sbrows setDelegate:self];
[sbrows searchForServicesOfType:@"_scijs._tcp" inDomain:@""];

And here are the delegate methods:

-(void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)aNetServiceBrowser {
    [self.statusLabel setStringValue:@"Searching for server..."];
    [self.serverIndicator setHidden:NO];
    [self.serverIndicator startAnimation:self];
}

-(void)netServiceBrowser:aNetServiceBrowser didNotSearch:(NSDictionary *)errorDict {
    [self.statusLabel setStringValue:@"Error finding server."];
    [self.serverIndicator stopAnimation:self];
    [self.serverIndicator setHidden:YES];
}

-(void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)aNetServiceBrowser {
    [self.serverIndicator stopAnimation:self];
    [self.serverIndicator setHidden:YES];
}

-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
    [aNetServiceBrowser stop];
    NSLog(@"Found server.");
    [self.statusLabel setStringValue:@"Resolving service..."];
    [aNetService setDelegate:self];
    [aNetService resolveWithTimeout:10.0];
}

Most of it's just GUI code and NSLogs, yet I never get the gui changes or the NSLogs. Does anyone have a solution?

Upvotes: 1

Views: 2243

Answers (1)

estobbart
estobbart

Reputation: 1145

Chances are it's in your client.

  1. Set breakpoints in the delegate methods.
  2. Make sure sbrows isn't falling out of scope. If it falls out of scope willSearch gets called then no other responses.

Upvotes: 2

Related Questions