linux之 获取主机IP gethostbyname()函数说明

gethostbyname()函数说明——用域名或主机名获取IP地址

    包含头文件
    #include <netdb.h>
    #include <sys/socket.h>

    函数原型
    struct hostent *gethostbyname(const char *name);
    这个函数的传入值是域名或者主机名,例如"www.google.cn"等等。传出值,是一个hostent的结构。如果函数调用失败,将返回NULL。

    返回hostent结构体类型指针
    struct hostent
    {
        char    *h_name;               
        char    **h_aliases;
        int     h_addrtype;
        int     h_length;
        char    **h_addr_list;
        #define h_addr h_addr_list[0]
    };

    hostent->h_name
    表示的是主机的规范名。例如www.google.com的规范名其实是www.l.google.com。
    
    hostent->h_aliases
    表示的是主机的别名.www.google.com就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。

    hostent->h_addrtype    
    表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是pv6(AF_INET6)

    hostent->h_length      
    表示的是主机ip地址的长度

    hostent->h_addr_lisst
    表示的是主机的ip地址,注意,这个是以网络字节序存储的。千万不要直接用printf带%s参数来打这个东西,会有问题的哇。所以到真正需要打印出这个IP的话,需要调用inet_ntop()。

    const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) :
    这个函数,是将类型为af的网络地址结构src,转换成主机序的字符串形式,存放在长度为cnt的字符串中。返回指向dst的一个指针。如果函数调用错误,返回值是NULL。

复制代码
 1  #include  < netdb.h >
 2  #include  < sys / socket.h >
 3  #include  < stdio.h >
 4 
 5  int  main( int  argc,  char   ** argv)
 6  {
 7       char     * ptr,  ** pptr;
 8       struct  hostent  * hptr;
 9       char    str[ 32 ];
10      ptr  =  argv[ 1 ];
11 
12       if ((hptr  =  gethostbyname(ptr))  ==  NULL)
13      {
14          printf( "  gethostbyname error for host:%s\n " , ptr);
15           return   0 ;
16      }
17 
18      printf( " official hostname:%s\n " ,hptr -> h_name);
19       for (pptr  =  hptr -> h_aliases;  * pptr  !=  NULL; pptr ++ )
20          printf( "  alias:%s\n " , * pptr);
21 
22       switch (hptr -> h_addrtype)
23      {
24           case  AF_INET:
25           case  AF_INET6:
26              pptr = hptr -> h_addr_list;
27               for (;  * pptr != NULL; pptr ++ )
28                  printf( "  address:%s\n " ,
29                         inet_ntop(hptr -> h_addrtype,  * pptr, str,  sizeof (str)));
30              printf( "  first address: %s\n " ,
31                         inet_ntop(hptr -> h_addrtype, hptr -> h_addr, str,  sizeof (str)));
32           break ;
33           default :
34              printf( " unknown address type\n " );
35           break ;
36      }
37 
38       return   0 ;
39  }
复制代码

编译运行
-----------------------------
# gcc test.c
# ./a.out www.baidu.com
official hostname:www.a.shifen.com
alias:www.baidu.com
address:121.14.88.11
address:121.14.89.11
first address: 121.14.88.11

感谢http://www.cnblogs.com/cxz2009/archive/2010/11/19/1881611.html



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. gethostbyname函数函数用于将主机名转换为IP地址。函数原型如下: ``` struct hostent *gethostbyname(const char *name); ``` 参数name为要转换的主机名,返回值为一个指向hostent结构体的指针。 示例代码: ``` #include <netdb.h> #include <stdio.h> int main(int argc, char *argv[]) { struct hostent *he; char *hostname = "www.baidu.com"; if ((he = gethostbyname(hostname)) == NULL) { printf("gethostbyname error\n"); return 1; } printf("Official name: %s\n", he->h_name); printf("IP addresses: "); for (int i = 0; he->h_addr_list[i] != NULL; i++) { printf("%s ", inet_ntoa(*(struct in_addr *)he->h_addr_list[i])); } printf("\n"); return 0; } ``` 运行结果: ``` Official name: www.a.shifen.com IP addresses: 14.215.177.38 14.215.177.39 ``` 2. gethostbyaddr函数函数用于将IP地址转换为主机名。函数原型如下: ``` struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type); ``` 参数addr为要转换的IP地址,len为地址长度,type为地址类型,一般为AF_INET。返回值为一个指向hostent结构体的指针。 示例代码: ``` #include <netdb.h> #include <stdio.h> int main(int argc, char *argv[]) { struct hostent *he; struct in_addr addr; if (inet_aton("14.215.177.38", &addr) == 0) { printf("inet_aton error\n"); return 1; } if ((he = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET)) == NULL) { printf("gethostbyaddr error\n"); return 1; } printf("Official name: %s\n", he->h_name); return 0; } ``` 运行结果: ``` Official name: www.a.shifen.com ``` 3. getservbyname函数函数用于将服务名转换为端口号。函数原型如下: ``` struct servent *getservbyname(const char *name, const char *proto); ``` 参数name为要转换的服务名,proto为协议名,一般为"tcp"或"udp"。返回值为一个指向servent结构体的指针。 示例代码: ``` #include <netdb.h> #include <stdio.h> int main(int argc, char *argv[]) { struct servent *se; char *servname = "http"; char *proto = "tcp"; if ((se = getservbyname(servname, proto)) == NULL) { printf("getservbyname error\n"); return 1; } printf("Service name: %s\n", se->s_name); printf("Port number: %d\n", ntohs(se->s_port)); return 0; } ``` 运行结果: ``` Service name: http Port number: 80 ``` 综上所述,这三种地址转换函数都能够在网络编程中起到很大的作用,例如在客户端连接服务器时需要将主机名转换为IP地址,而在服务器端接受连接时需要将IP地址转换为主机名。在使用时需要注意传入的参数类型和返回值类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值