Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1184973
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-02 15:35:16


点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <netdb.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>

  7. int main(int argc, char * * argv)
  8. {
  9.     printf("sethostent函数执行:\n");
  10.     sethostent(1);
  11.     printf("-1.-1**************************************\n");
  12.     struct hostent * host = NULL;
  13.     host = gethostent();
  14.     printf("h_name =[%s]\n", host->h_name);
  15.     printf("*h_aliases =[%s]\n", *(host->h_aliases));
  16.     printf("h_addrtype =[%d]\n", host->h_addrtype);
  17.     printf("h_length =[%d]\n", host->h_length);
  18.     printf("h_addr_list =[%s]\n", host->h_addr_list[0]);
  19.     printf("h_addr =[%s]\n", host->h_addr);
  20.     
  21.     struct sockaddr_in sin;
  22.     bzero(&sin, sizeof(sin));
  23.     bcopy(host->h_addr, &sin.sin_addr, host->h_length);
  24.     char ipbuf[128];
  25.     memset(ipbuf, 0, sizeof(ipbuf));
  26.     inet_ntop(AF_INET, &sin.sin_addr.s_addr, ipbuf, sizeof(ipbuf));
  27.     printf("主机名=[%s],IP地址=[%s]\n",host->h_name,ipbuf);
  28.     
  29.     bzero(&sin, sizeof(sin));
  30.     memcpy(&sin.sin_addr, host->h_addr, host->h_length);
  31.     memset(ipbuf, 0, sizeof(ipbuf));
  32.     inet_ntop(AF_INET, &sin.sin_addr.s_addr, ipbuf, sizeof(ipbuf));
  33.     printf("主机名=[%s],IP地址=[%s]\n",host->h_name,ipbuf);
  34.     
  35.     printf("setservent 函数执行:\n");
  36.     setservent(1);
  37.     printf("000000**************************************\n");
  38.     struct servent * serv0 = NULL;
  39.     serv0 = getservent();
  40.     if(serv0 != NULL)
  41.     {
  42.         printf("s_name =[%s]\n", serv0->s_name);
  43.         printf("s_aliases[0]=[%s]\n", serv0->s_aliases[0]);
  44.         printf("s_port =[%d]\n", serv0->s_port);
  45.         printf("s_proto =[%d]\n", serv0->s_proto);
  46.     }
  47.     else
  48.     {
  49.         printf("serv0 is null\n");
  50.     }
  51.     
  52.     printf("111111**************************************\n");

  53.     struct addrinfo hint;
  54.   hint.ai_flags = AI_CANONNAME; /* AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV */
  55.   hint.ai_family = 0;
  56.   hint.ai_socktype = 0;
  57.   hint.ai_protocol = 0;
  58.   hint.ai_addrlen = 0;
  59.   hint.ai_addr = NULL;
  60.   hint.ai_canonname = NULL;
  61.   hint.ai_next = NULL;
  62.   
  63.     /*bzero( &hint, sizeof(struct addrinfo));*/
  64.     
  65.     printf("222222**************************************\n");
  66.     struct addrinfo * result = NULL;
  67.     printf("333333**************************************\n");
  68.     /*getaddrinfo("wangxczj", NULL, &hint, &result);*/
  69.     /*
  70.     getaddrinfo(NULL, "tcpmux", &hint, &result);
  71.     */
  72.     getaddrinfo("YH.localdomain", NULL, &hint, &result);
  73.     printf("result->ai_flags=[%d]\n", result->ai_flags);
  74.     printf("result->ai_family=[%d]\n", result->ai_family);
  75.     printf("result->ai_socktype=[%d]\n", result->ai_socktype);
  76.     printf("result->ai_protocol=[%d]\n", result->ai_protocol);
  77.     printf("result->ai_addrlen=[%d]\n", result->ai_addrlen);
  78.     
  79.     struct sockaddr_in seraddr;
  80.     memset(&seraddr, 0, sizeof(seraddr));
  81.     memcpy(&seraddr, result->ai_addr, result->ai_addrlen);
  82.     memset(ipbuf, 0, sizeof(ipbuf));
  83.     inet_ntop(AF_INET, &seraddr.sin_addr.s_addr, ipbuf, sizeof(ipbuf));
  84.     printf("IP地址=[%s]\n", ipbuf);
  85.     printf("PORT=[%s]\n", ntohs(seraddr.sin_port));
  86.     
  87.     printf("result->ai_canonname=[%s]\n", result->ai_canonname);
  88.     if(result->ai_next == NULL)
  89.     {
  90.         printf("result->ai_next is null!\n");
  91.     }
  92.     else
  93.     {
  94.         printf("result->ai_next is not null!\n");
  95.     }
  96.     printf("444444**************************************\n");
  97.     struct addrinfo * result2 = result->ai_next;
  98.     printf("result2->ai_flags=[%d]\n", result2->ai_flags);
  99.     printf("result2->ai_family=[%d]\n", result2->ai_family);
  100.     printf("result2->ai_socktype=[%d]\n", result2->ai_socktype);
  101.     printf("result2->ai_protocol=[%d]\n", result2->ai_protocol);
  102.     printf("result2->ai_addrlen=[%d]\n", result2->ai_addrlen);
  103.     
  104.     struct sockaddr_in seraddr2;
  105.     memset(&seraddr2, 0, sizeof(seraddr2));
  106.     memcpy(&seraddr2, result2->ai_addr, result2->ai_addrlen);
  107.     memset(ipbuf, 0, sizeof(ipbuf));
  108.     inet_ntop(AF_INET, &seraddr2.sin_addr.s_addr, ipbuf, sizeof(ipbuf));
  109.     printf("IP地址=[%s]\n", ipbuf);
  110.     printf("PORT=[%s]\n", ntohs(seraddr2.sin_port));
  111.     
  112.     printf("result2->ai_canonname=[%s]\n", result2->ai_canonname);
  113.     if(result2->ai_next == NULL)
  114.     {
  115.         printf("result2->ai_next is null!\n");
  116.     }
  117.     else
  118.     {
  119.         printf("result2->ai_next is not null!\n");
  120.     }
  121.     
  122.     
  123.     
  124.     printf("555555**************************************\n");
  125.     struct addrinfo * result3 = result2->ai_next;
  126.     printf("result3->ai_flags=[%d]\n", result3->ai_flags);
  127.     printf("result3->ai_family=[%d]\n", result3->ai_family);
  128.     printf("result3->ai_socktype=[%d]\n", result3->ai_socktype);
  129.     printf("result3->ai_protocol=[%d]\n", result3->ai_protocol);
  130.     printf("result3->ai_addrlen=[%d]\n", result3->ai_addrlen);
  131.     
  132.     struct sockaddr_in seraddr3;
  133.     memset(&seraddr3, 0, sizeof(seraddr3));
  134.     memcpy(&seraddr3, result3->ai_addr, result3->ai_addrlen);
  135.     memset(ipbuf, 0, sizeof(ipbuf));
  136.     inet_ntop(AF_INET, &seraddr3.sin_addr.s_addr, ipbuf, sizeof(ipbuf));
  137.     printf("IP地址=[%s]\n", ipbuf);
  138.     printf("PORT=[%s]\n", ntohs(seraddr3.sin_port));
  139.     
  140.     printf("result3->ai_canonname=[%s]\n", result3->ai_canonname);
  141.     if(result3->ai_next == NULL)
  142.     {
  143.         printf("result3->ai_next is null!\n");
  144.     }
  145.     else
  146.     {
  147.         printf("result3->ai_next is not null!\n");
  148.     }
  149.     
  150.     
  151.     printf("666666**************************************\n");
  152.     struct addrinfo * result4 = result3->ai_next;
  153.     printf("result4->ai_flags=[%d]\n", result4->ai_flags);
  154.     printf("result4->ai_family=[%d]\n", result4->ai_family);
  155.     printf("result4->ai_socktype=[%d]\n", result4->ai_socktype);
  156.     printf("result4->ai_protocol=[%d]\n", result4->ai_protocol);
  157.     printf("result4->ai_addrlen=[%d]\n", result4->ai_addrlen);
  158.     
  159.     struct sockaddr_in seraddr4;
  160.     memset(&seraddr4, 0, sizeof(seraddr4));
  161.     memcpy(&seraddr4, result4->ai_addr, result4->ai_addrlen);
  162.     memset(ipbuf, 0, sizeof(ipbuf));
  163.     inet_ntop(AF_INET, &seraddr4.sin_addr.s_addr, ipbuf, sizeof(ipbuf));
  164.     printf("IP地址=[%s]\n", ipbuf);
  165.     
  166.     printf("result4->ai_canonname=[%s]\n", result4->ai_canonname);
  167.     if(result4->ai_next == NULL)
  168.     {
  169.         printf("result4->ai_next is null!\n");
  170.     }
  171.     else
  172.     {
  173.         printf("result4->ai_next is not null!\n");
  174.     }
  175.     
  176.     return 0;
  177. }

阅读(387) | 评论(0) | 转发(0) |
0

上一篇:epoll函数

下一篇:getnameinfo函数

给主人留下些什么吧!~~