其他分享
首页 > 其他分享> > 根据域名获取ip地址gethostbyname

根据域名获取ip地址gethostbyname

作者:互联网

#include <sys/socket.h>
#include <stdio.h>
#include <netdb.h>

int main(int argc, char **argv)
{
    char   *ptr, **pptr;
    struct hostent *hptr;
    char   str[32];
    ptr = argv[1];

    hptr = gethostbyname(ptr);
    if(NULL == hptr)
    {
        printf(" gethostbyname error\n");
        return 0;
    }

    printf("hostname:%s\n", hptr->h_name);
    
    for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
        printf(" pptr:%s\n",*pptr);

    switch(hptr->h_addrtype)
    {
        case AF_INET:
        case AF_INET6:
            pptr=hptr->h_addr_list;
            for(; *pptr!=NULL; pptr++)
            {
                inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str));
                printf("address: %s\n", str);
            }

        break;
    }

    return 0;
}

 

标签:ip,pptr,hptr,char,gethostbyname,域名,str,printf,NULL
来源: https://www.cnblogs.com/jly594761082/p/10748915.html