#https://blog.csdn.net/dosthing/article/details/80383817
std::string ip = "www.baidu.com";
struct hostent stHostent;
struct hostent *pstHostent = nullptr;
char **pptr = nullptr;
char buf[2048] = "\0";
sprintf(buf,"%s", "ip");
int iError;
if((pstHostent = gethostbyname(buf)) == NULL || pstHostent->h_addr_list == NULL || pstHostent->h_addr_list[0] == NULL)
{
std::cout << "can't parse the domain" << std::endl;
exit(0);
}
else
{
std::cout << "official hostname: " << pstHostent->h_name << std::endl;
for(pptr = pstHostent->h_aliases; *pptr != NULL; pptr++)
std::cout << "alias: " << *pptr << std::endl;
switch(pstHostent->h_addrtype)
{
case AF_INET:
case AF_INET6:
pptr = pstHostent->h_addr_list;
for(; *pptr!=NULL; pptr++)
printf("address:%s\n",inet_ntop(pstHostent->h_addrtype, *pptr, buf,64));
printf("first address: %s\n",inet_ntop(pstHostent->h_addrtype, pstHostent->h_addr,buf,64));
break;
default:
printf("Unkown address type\n");
}
}