How do I get the IP from a given hostname on iOS?
I've tried Google but found nothing.
How do I get the IP from a given hostname on iOS?
I've tried Google but found nothing.
The following code works with both IPv4 and IPv6. It uses getaddrinfo()
to
retrieve a list of IP addresses for a host, and getnameinfo()
to convert
each IP address into a string. (Error checking omitted for brevity.)
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC; // PF_INET if you want only IPv4 addresses
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo *addrs, *addr;
getaddrinfo("www.google.com", NULL, &hints, &addrs);
for (addr = addrs; addr; addr = addr->ai_next) {
char host[NI_MAXHOST];
getnameinfo(addr->ai_addr, addr->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
printf("%s\n", host);
}
freeaddrinfo(addrs);