Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IP from hostname on iOS [duplicate]

How do I get the IP from a given hostname on iOS?

I've tried Google but found nothing.

like image 761
Hedam Avatar asked Feb 04 '26 18:02

Hedam


1 Answers

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);
like image 52
Martin R Avatar answered Feb 06 '26 08:02

Martin R