PDA

View Full Version : NSNetService works, but hostName is null and port is 0




RossOliver
Jul 17, 2008, 04:18 PM
Hey,

I create an NSNetService:


service = [[NSNetService alloc] initWithDomain:@"" type:@"_myprog._tcp" name:@"My Prog Server" port:28496];
[service setDelegate:self];


Which works fine, then in the client side I browse for the service and in the delegate method:


-( void )netServiceBrowser:( NSNetServiceBrowser * )netServiceBrowser
didFindService:( NSNetService * )netService moreComing:( BOOL )moreComing
{
printf( "desc: %s, name: %s, host: %s, port: %i\n", [[netService description] UTF8String], [[netService name] UTF8String], [[netService hostName] UTF8String], [netService port] );
}


It seems to descover the service perfectly, but for some reason thinks the host name is null and the port is 0 instead of 28496:


desc: <NSNetService 0x44d800> local. _myprog._tcp. My Prog Server, name: My Prog Server, host: (null), port: 0


Any ideas why this would happen?

Thanks for your time,

-Ross



RossOliver
Jul 18, 2008, 02:09 AM
I read that I need to resolve the host and port with resolveWithTimeout: so I added this to the method when a service is found:


[netService setDelegate:self];
[netService resolveWithTimeout:5];


I implemented netService:didNotResolve: and netserviceDidResolveAddress: but they don't appear to be called at all :confused:


I also notice that the [netService addresses] returns an empty array...

-Ross

Manty
Jul 18, 2008, 03:52 AM
You are retaining the incoming service? That might do the trick, i put them in one array in my application.


Manty

RossOliver
Jul 18, 2008, 03:56 AM
You are retaining the incoming service? That might do the trick, i put them in one array in my application.


Manty

At the moment I'm not - I will give that a try. I also read in the documentation that I have to use initWithDomain@"local." instead of @"" for the resolution process to work, so I will give that a go too...

Thanks for the suggestion

RossOliver
Jul 18, 2008, 02:16 PM
You are retaining the incoming service? That might do the trick, i put them in one array in my application.


Manty

You were spot on - I had to retain the service. It must have been discarded at the end of the method, which makes sense since it's an automatic variable :rolleyes:

Cheers