Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNetServiceBrowser did NOT find published service

Tags:

objective-c

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:@""];
like image 646
Scofield Tran Avatar asked Nov 23 '12 10:11

Scofield Tran


3 Answers

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

like image 146
Hari Honor Avatar answered Dec 11 '22 08:12

Hari Honor


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.

like image 40
Shamsudheen TK Avatar answered Dec 11 '22 10:12

Shamsudheen TK


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

like image 20
Bamaco Avatar answered Dec 11 '22 08:12

Bamaco