Greetings,
I am having some trouble writing a method that can reliably resolve an IP address to host name.
I have attempted to use 2 different techniques: NSHost and CFHost. Both of these techniques appear to have the same difficulty resolving a hostname after a network transition has occurred.
An example, in it's simplest form:
// Resolve a single IP address and return the first hostname // Yes, this blocks. No, this is not thread safe. - (NSString *) resolveIPaddress:(NSString *)ipAddress NSHost *host = [NSHost hostWithAddress:ipAddress]; name = [host name]; if (name == nil) { NSLog(@"name is nil"); name = @"<name_resolution_error>"; // Using CFHost the error returned is: 12,8 } If I run the code above, sometimes the name is returned, sometimes it is not.
If the name _is_ returned, forcing some kind of network transition, downing an interface for example, and running the code again will produce the expected result: a nil name.
Now, if I bring the interface up again and run the code, I would expect that ipAddress *should* resolve -- it does not.
Again, this happens whether or not I use any of the above techniques, synchronously or asynchronously.
I am fairly new to programming -- perhaps I lack some fundamental understanding here?
Hoping someone can enlighten me.
Thanks!
-- Brian W. |