gethostbyname 与 gethsotbyaddr

1.数据结构hostentservent:

1.  struct hostent{

2.     char *h_name;   /*official domain name of host */

3.     char **h_aliases; /* null-terminated array of domainnames */

4.     int h_addrtype;  /* host address type (AF_INET) */

5.     int h_length;    /* length of an address, in bytes */

6.     char **h_addr_list;/* null-terminated array of in_addrstructs */

7.  };

8.  #define h_addr h_addr_list[0]  

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


char *h_name :表示的是主机的规范名。例如www.google.com的规范名其实是www.l.google.com。
char **h_aliases: 表示的是主机的别名。www.google.com就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。
int h_addrtype :表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是ipv6(AF_INET6)
int h_length :表示的是主机ip地址的长度
int **h_addr_lisst: 表示的是主机的ip地址,注意,这个是以网络字节序存储的。千万不要直接用printf带%s参数来打这个东西,会有问题的哇。所以到真正需要打印出这个IP的话,需要调用inet_ntop()。

结构体servent

1.  typedef struct servent {

2.  char FAR* s_name; //正规的服务名

3.  char FAR FAR** s_aliases;

4.  short s_port; //连接到该服务时需要用到的端口号

5.  char FAR* s_proto;

6.  } servent;

2.取得主机名与域名
2.1 gethostname() : 返回本地主机的标准主机名。
原型如下:

1.  #include <unistd.h>

2.  int gethostname(char *name, size_t len);

参数说明:
这个函数需要两个参数:
接收缓冲区name,其长度必须为len字节或是更长
接收缓冲区name的最大长度
返回值:
如果函数成功,则返回0。如果发生错误则返回-1。错误号存放在外部变量errno中。
2.2 getdomainname()

函数:可以允许程序获得程序正运行的主机的NIS域名。
原型如下:

1.  #include <unistd.h>

2.  int getdomainname(char *name,size_t len);

参数说明:
这个函数的用法也gethostname相同。
2.3 测试getdomainname与gethostname函数
下面这个程序演示了这两个函数的用法。这个程序只是简单的调用这两个函数并报告其结果。

/*gethostname.c

 *

 * Example ofgethostname(2):

 */

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <errno.h>

int main(int argc,char **argv)

        

{

    int z;

    charbuf[32];

    z =gethostname(buf,sizeof buf);

    if(z==-1)

    {

   fprintf(stderr,"%s:gethostname(2)\n",strerror(errno));

    exit(1);

           }

   printf("host name = '%s'\n",buf);

    z =getdomainname(buf,sizeof buf);

           if(z==-1)

    {

   fprintf(stderr,"%s:getdomainname(2)\n",strerror(errno));

           exit(1);

    }

   printf("domain name = '%s'\n",buf);

    return 0;

}

 

这个程序的运行结果如下:
                             


在了解了如何获得本地系统的信息以后,现在我们就可以将我们的注意力转移到解析远程主机名上了。


3.gethostbyaddr():
struct hostent *gethostbyaddr(const char *name)
这个函数,传入值是IP地址(注意,这里不是简单的字符串,需要先将字符串形式的IP地址由inet_aton转化一下),然后经过函数处理,将结果由返回 值传出。返回值是一个hostent结构.因为有了hosten这个传出的结构,我们可以在这个结构里面找到我们想需要的信息。
下面的是例程。

#include <netdb.h>

#include <sys/socket.h>

#include <stdio.h>

int main(int argc, char **argv)

{

       char*ptr,**pptr;

       structhostent *hptr;

       charstr[32];

       structin_addr *hipaddr;

 

 /* 取得命令后第一个参数,即要解析的IP地址 */

       ptr =argv[1];

 

       unsignedlong addr = inet_addr(ptr);

 /* 调用gethostbyaddr()。调用结果都存在hptr中 */

       if( (hptr= gethostbyaddr((char *)&addr), 4, AF_INET) ) == NULL ){

              printf("gethostbyaddrerror for addr:%s\n", ptr);

              return1; /* 如果调用gethostbyaddr发生错误,返回1 */

       }

 

 /* 将主机的规范名打出来 */

       printf("officialhostname:%s\n",hptr->h_name);

 

 /* 主机可能有多个别名,将所有别名分别打出来 */

       for(pptr =hptr->h_aliases; *pptr != NULL; pptr++)

       printf("  alias:%s\n",*pptr);

 /* 根据地址类型,将地址打出来 */

       switch(hptr->h_addrtype){

              caseAF_INET:

              caseAF_INET6:

                     pptr=hptr->h_addr_list;

   /* 将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数 */

                     for(;*pptr!=NULL;pptr++)

                     printf("  address:%s\n",inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));

              break;

              default:

                     printf("unknownaddress type\n");

              break;

       }

       return 0;

}

编译后只需在命令行输入 ./gethostbyaddr  202.102.14.141 (IP地址)就可以看结果了。

4.gethostbyname():
使用这个东西,首先要包含2个头文件:

1.  #include <netdb.h>

2.  #include <sys/socket.h>

3.  struct hostent *gethostbyname(const char *name);

这个函数的传入值是域名或者主机名,例如"www.baidu.com","wpc"等等。
传出值,是一个hostent的结构(如下)。如果函数调用失败,将返回NULL。

1.  struct hostent

2.  {

3.    char  *h_name;

4.    char **h_aliases;

5.    int   h_addrtype;

6.    int   h_length;

7.    char  **h_addr_list;

8.  };

(对它的解释见1)

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.  int main(int argc, char **argv)

4.  {

5.   char *ptr,**pptr;

6.   struct hostent*hptr;

7.   char str[32];

8.   /* 取得命令后第一个参数,即要解析的域名或主机名 */

9.   ptr = argv[1];

10. /* 调用gethostbyname()。调用结果都存在hptr中 */

11. if( (hptr = gethostbyname(ptr) ) == NULL )

12. {

13.  printf("gethostbyname errorfor host:%s\n", ptr);

14.  return 0; /* 如果调用gethostbyname发生错误,返回1 */

15. }

16.

17. /* 将主机的规范名打出来 */

18. printf("official hostname:%s\n",hptr->h_name);

19. /* 主机可能有多个别名,将所有别名分别打出来 */

20. for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)

21.  printf("  alias:%s\n",*pptr);

22. /* 根据地址类型,将地址打出来 */

23. switch(hptr->h_addrtype)

24. {

25.  case AF_INET:

26.  case AF_INET6:

27.   pptr=hptr->h_addr_list;

28.   /* 将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数 */

29.   for(;*pptr!=NULL;pptr++)

30.    printf("  address:%s\n",inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));

31.   break;

32.  default:

33.   printf("unknown addresstype\n");

34.   break;

35. }

36. return 0;

37.}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值