1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

NSNetService works, but hostName is null and port is 0

Discussion in 'iPhone/iPad Programming' started by RossOliver, Jul 17, 2008.

  1. macrumors regular

    #1
    Hey,

    I create an NSNetService:

    Code:
    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:

    Code:
    -( 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,

    -Ross
     
  2. macrumors regular

    #2
    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:

    Code:
    [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
     
  3. macrumors member

    #3
    You are retaining the incoming service? That might do the trick, i put them in one array in my application.


    Manty
     
  4. macrumors regular

    #4
    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. macrumors regular

    #5
    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
     

Share This Page