打印hostent结构体中的所有内容

来源:互联网 发布:大学生谈恋爱 知乎 编辑:程序博客网 时间:2024/05/01 07:37

问题:

hostent是gethostbyname()和gethostbyaddr()都会涉及到的一个结构体。代码如下:

struct hostent {
  char  *h_name;
  char  **h_aliases;
  int   h_addrtype;
  int   h_length;
  char  **h_addr_list;
  };

gethostbyname()的原型是:

struct hostent *gethostbyname(const char *name);
函数的传入值是域名或者主机名,例如"www.google.com"、"191.168.1.253"等;
函数的返回值:

成功时,返回一个指向hostent结构的指针;

失败时,返回NULL。

编程调试过程中需要查看所取到的IP地址等信息,所以将hostent的全部内容打印出来就对调试很有帮助了。


解决办法:

[cpp] view plaincopyprint?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <netdb.h>  
  4. #include <sys/socket.h>  
  5.   
  6. int main(int argc, char *argv[])  
  7. {  
  8.     char *ptr,**pptr;  
  9.     struct hostent *host;  
  10.     char str[32];  
  11.     char *hostname;  
  12.   
  13.     if (argc < 2) {  
  14.         fprintf(stderr, "USAGE: ./pri-hostent Hostname(of ip addr such as 192.168.1.101)\n");  
  15.         exit(1);  
  16.     }  
  17.   
  18.     /* get the host name */  
  19.     hostname = argv[1];  
  20.   
  21.     // if ((host = (struct hostent*)gethostbyname(argv[1])) == NULL) {  
  22.     if ((host = (struct hostent*)gethostbyname(hostname)) == NULL) {  
  23.         perror("gethostbyname");  
  24.         return(1);  
  25.     }  
  26.     printf("Host name is: %s\n", host->h_name);  
  27.     /* printf all the aliases of host */  
  28.     for(pptr = host->h_aliases; *pptr != NULL; pptr++) {  
  29.         printf("  alias of host: %s\n",*pptr);  
  30.     }  
  31.     printf("Host addrtype is: %d\n", host->h_addrtype);  
  32.     printf("Host length is: %d\n", host->h_length);  
  33.     // // printf("Host is: %x\n", *((struct in_addr*)host->h_addr));  
  34.     /* printf ip address according to addrtype */  
  35.     switch(host->h_addrtype) {  
  36.         case AF_INET:  
  37.         case AF_INET6:  
  38.             pptr = host->h_addr_list;  
  39.             /* print all the aliases,inet_ntop() is called */  
  40.             for(; *pptr!=NULL; pptr++) {  
  41.                 inet_ntop(host->h_addrtype, *pptr, str, sizeof(str));  
  42.                 printf("  address: %s\n", str);  
  43.             }  
  44.         break;  
  45.         default:  
  46.             printf("unknown address type\n");  
  47.         break;  
  48.     }  
  49.   
  50.     return 0;  
  51. }  

程序的运行结果:

[cpp] view plaincopyprint?
  1. dingq@wd-u1110:~/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/isocket$ ./pri-hostent   
  2. USAGE: ./pri-hostent Hostname(of ip addr such as 192.168.1.101)  
  3. dingq@wd-u1110:~/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/isocket$ ./pri-hostent www.google.com  
  4. Host name is: www.l.google.com  
  5.   alias of host: www.google.com  
  6. Host addrtype is: 2  
  7. Host length is: 4  
  8.   address: 74.125.128.106  
  9.   address: 74.125.128.147  
  10.   address: 74.125.128.99  
  11.   address: 74.125.128.103  
  12.   address: 74.125.128.104  
  13.   address: 74.125.128.105 
0 0