struct hostent结构体

结构体定义:

struct hostent
{
    char *h_name;   //正式主机名
    char **h_aliases;  //主机别名
    int h_addrtype;   //主机IP地址类型
    int h_length;       //主机IP地址字节长度
    char **h_addr_list; //主机的IP地址列表          
}
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>

int main(int argc, char *argv[])
{
    char *ptr, **pptr;
    struct hostent *hptr;
    char str[32] = {0};
    
    ptr = argv[1];
    
    if((hptr = gethostbyname(ptr)) == NULL)
    {
        printf("gethostbyname error: %s
", ptr);
        return 0;
    }
    
    printf("official hostname:%s
", hptr->h_name);   //主机规范名
    
    for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)   //将主机别名打印出来
        printf("alias: %s
", *pptr);
    
    switch(hptr->h_addrtype)  //根据地址类型,将地址打印出来
    {
        case AF_INET:
        case AF_INET6:
            pptr = hptr->h_addr_list;
        
            for(; *pptr != NULL; pptr++)   //将得到的所有地址打印出来
            {
                printf("address: %s
", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));   //inet_ntop: 将网络字节序的二进制转换为文本字符串的格式
                printf("first address: %s
", inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));
            }
            break;
        default:
            printf("unkown address type
");
            break;
    }
    
    return 0;
}

运行结果:

official hostname:www.a.shifen.com
alias: www.baidu.com
address: 115.239.211.112
first address: 115.239.211.112
address: 115.239.210.27
first address: 115.239.211.112

原文地址:https://www.cnblogs.com/killer-xc/p/6610058.html