struct hostent 结构体与gethostbyname(),gethostbyaddr

    技术2022-05-19  19

    1、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]

     

    这里是这个数据结构的详细资料:

     struct hostent: h_name – 地址的正式名称。

     h_aliases – 空字节-地址的预备名称的指针,也可以这样说 一个以空指针结尾的可选主机名队列。 h_addrtype –地址类型; 通常是AF_INET。

     h_length – 地址的比特长度。

    h_addr_list – 零字节-主机网络地址指针( 空指针结尾的主机地址的列表,返回的地址是以网络顺序排列的)。网络字节顺序 h_addr - h_addr_list中的第一地址。

     

     2、 gethostbyname 原形:

    struct hostent *gethostbyname( const char *name );

    gethostbyname() 成功时返回一个指向结构体 hostent 的指针,或者 是个空 (NULL) 指针。(但是和以前不同,不设置errno,h_errno 设置错 误信息。请看下面的 herror()。) 但是如何使用呢? 这个函数可不象它看上去那么难用。

     

     这里是个例子:

     

    #include<stdio.h>  #include<stdlib.h>

    #include<errno.h>

    #include <netdb.h>

    #include <sys/types.h>

    #include<netinet/in.h>

     int main(int argc, char *argv[])

     {

        struct hostent *h;

       if (argc != 2)

       {

       /* 检查命令行 */

        fprintf(stderr,"usage: getip address/n");

        exit(1);

       }

       if ((h=gethostbyname(argv[1])) == NULL)

        {

         /* 取得地址信息 */

         herror("gethostbyname");

         exit(1);

        }

          printf("Host name : %s/n", h->h_name);

          printf("IP Address : %s/n",inet_ntoa(*((struct in_addr *)h->h_addr)));

         return 0;

        }

     

        在使用 gethostbyname() 的时候,你不能用perror() 打印错误信息 (因为 errno 没有使用),你应该调用 herror()。相当简单,你只是传递一个保存机器名的字符串(例如 "whitehouse.gov") 给gethostbyname(),然后从返回的数据结构 struct hostent 中获取信息。 唯一也许让人不解的是输出 IP 地址信息。h->h_addr 是一个 char *, 但是 inet_ntoa() 需要的是 struct in_addr。因此,我转换 h->h_addr 成 struct in_addr *,然后得到数据。

     

    3 gethostbyaddr gethostbyaddr 返回机器名称。

        语法: struct hosten *gethostbyaddr(const char * name);

        name 或者为主机名,或者是ipv4/ipv6地址。

     

     


    最新回复(0)