NSNetService works, but hostName is null and port is 0

Discussion in 'iOS Programming' started by RossOliver, Jul 17, 2008.

  1. RossOliver macrumors regular

    Nov 6, 2006

    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:

    Any ideas why this would happen?

    Thanks for your time,

  2. RossOliver thread starter macrumors regular

    Nov 6, 2006
    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...

  3. Manty macrumors member

    Mar 18, 2008
    Lisbon, Portugal
    You are retaining the incoming service? That might do the trick, i put them in one array in my application.

  4. RossOliver thread starter macrumors regular

    Nov 6, 2006
    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
  5. RossOliver thread starter macrumors regular

    Nov 6, 2006
    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:


Share This Page