mercredi 28 janvier 2015

Resolve string of hostname to string of IP


I am making an iPhone app and I need to resolve a string of a hostname into a string of an IP address. For example, "MyComputer.local" -> "192.168.0.7". I have tried doing a few things, but none have worked. This is what I have right now:



struct hostent *hostentry;
hostentry = gethostbyname("MyComputer.local");
char *ipbuf = NULL;
inet_ntop(AF_INET, hostentry->h_addr_list[0], ipbuf, hostentry->h_length);
ipAddress = [NSString stringWithFormat:@"%s" , ipbuf];


For some reason, it always crashes on the inet_ntop, and yes, I am using an existing hostname to test. Thanks!





Aucun commentaire:

Enregistrer un commentaire