How can I get the IP-address from a domain name with the top level domain name? In this example get the IP of google.com. And if possible in IPv6 in the correct format.
This is what I've tried so far:
#include <netdb.h>
using namespace std;
int main()
{
struct hostent* myHostent;
myHostent = gethostbyname("google.com");
cout<<myHostent <<"\n";
//output is a hex code
cout<<myHostent->h_name<<"\n";
//output is google.com
cout<<myHostent->h_aliases;
//output is a hex code
}
The domain's IP addresses (yes plural, there can be more than 1) is in the
hostent::h_addr_listfield, not in thehostent::h_aliasesfield, eg:That said,
gethostbyname()is deprecated, usegetaddrinfo()instead, eg: