Scofield Tran
Scofield Tran

Reputation: 840

NSNetServiceBrowser did NOT find published service

On an iPhone (the server), I've tried to publish a service and my code ran into the NSNetService object's delegate method:

-(void)netServiceDidPublish:(NSNetService *)sender

So I believe that my service @"_chatty._tcp." has published successfully. Then on another iPhone (the client), I use NSNetServiceBrowser to find my service, but it did NOT run into the delegate method:

-(void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing

I found some questions related to my case on this site, most of the answer remind to check the delegate object whether is out of scope or not. I'm sure my delegate work well because it ran into another delegate method like:

-(void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)aNetServiceBrowser

Can anybody help me find out the reason?

Here are some parts of my code:

I init the service like that:

#define MY_PROTOCOL @"_chatty._tcp."

self.myService = [[NSNetService alloc]
                   initWithDomain:@"" type:MY_PROTOCOL
                   name:@"thaith" port:self.port];

The port is initialized with a given listeningSocket in the Browser class:

NSNetServiceBrowser* finder = [[NSNetServiceBrowser alloc] init];

//I also retain the finder.
finder.delegate = self;

[finder searchForServicesOfType:MY_PROTOCOL inDomain:@""];

Upvotes: 6

Views: 2982

Answers (4)

Bamaco
Bamaco

Reputation: 590

Instead of downloading bonjour browser, I suggest using the terminal command:

dns-sd -B _chatty._tcp local.

For me, it shows that the server side is working fine. Currently, I can find the service when my application starts, my only issue is that once I stop the server, I get the "removed" event but running it again, I cant discover it anymore. I know the problem is on my client side, thanks to dns-sd - B

Upvotes: 1

Hari Honor
Hari Honor

Reputation: 8914

After having come across the same problem and giving up for a month. I've just come back to it and solved it:

Even though the sample code in the docs seems to imply otherwise, don't use a local variable for the NSNetServiceBrowser. As soon as it goes out of scope it gets garbage collected. Make finder an instance variable or property so its sticks around. I didn't spot this straight away as the netServiceBrowserWillSearch: delegate was getting called so I assumed everything was ok...

Upvotes: 10

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

Possible Solutions

  1. Check both WiFi identifiers are same
  2. Check both are in same WiFi network
  3. Check the NSNetServiceBrowser delegate assigned as same class

At last download sample Apple.Developer Witap Application , install in two devices , test and confirm it working.

Upvotes: 1

Davyd Geyl
Davyd Geyl

Reputation: 4623

I would narrow the scope and try to find the problem place. First, find out whether the service is published correctly. Use Bonjour Browser application (you can find it in the Internet) on a computer within the same local network where you publish the service. I hope you publish and browse in the same local net. If the Bonjour Browser can see your service then you know it is published correctly. Then work on the browser side to connect to it.

Upvotes: 0

Related Questions