1. main.c 
  2. -------------------------------------------------------- 
  3.  #include <sys/types.h>       
  4.  #include <sys/socket.h> 
  5.  #include <stdio.h> 
  6.  #include <stdlib.h> 
  7.  #include <netdb.h> 
  8.  #include <string.h> 
  9.  #include <arpa/inet.h> 
  10.  #include "socket.h" 
  11.  
  12. extern int h_errno; 
  13.         
  14. int main(int argc, char** argv) 
  15.  
  16.     int ret; 
  17.      
  18. //通常,在给定的协议族中 已存在协议进行对特定的socket进行支持,所以 protocol设置为0 
  19.     int fd_sock = socket(AF_INET, SOCK_STREAM, 0); 
  20.     if(fd_sock == -1) 
  21.     { 
  22.         perror("sock\n"); 
  23.         exit(1); 
  24.     } 
  25.  
  26. //测试gethostent函数 
  27.     test_gethostent(); 
  28.     putchar('\n'); 
  29. //测试getnetent函数  
  30.     test_getnetent(); 
  31.      
  32.     ret = shutdown(fd_sock, SHUT_RDWR); 
  33.     if(ret == -1) 
  34.     { 
  35.         perror("shutdow\n"); 
  36.         exit(1); 
  37.     } 
  38. #if 0 
  39. close 与 shutdown的区别 
  40. close 只会在最后一个引用被释放时才会关闭socket描述符 
  41. shutdow 不论什么状态都会关闭,即使有多个引用此socket 
  42. shutdow 还可以选择性关闭 读端还是写端 
  43. close 只能读写端一起关闭 
  44. #endif 
  45.      
  46.     return 0; 
  47.  
  48. -------------------------------------------------------- 
  49. socket.c 
  50. -------------------------------------------------------- 
  51.  #include <sys/types.h>       
  52.  #include <sys/socket.h> 
  53.  #include <stdio.h> 
  54.  #include <stdlib.h> 
  55.  #include <netdb.h> 
  56.  #include <string.h> 
  57.  #include <arpa/inet.h> 
  58.  #include "socket.h" 
  59.  
  60.  
  61.  
  62. void show_hostent(struct hostent* host) 
  63.      
  64.      
  65.     printf("hostname: %s\n",host->h_name); 
  66.      
  67.     while(1) 
  68.     { 
  69.         if(*host->h_aliases == NULL) 
  70.         { 
  71.             printf("hostaliases: NULL\n"); 
  72.             break; 
  73.         } 
  74.         if(strcmp(*host->h_aliases,"(null)") == 0) 
  75.         { 
  76.             printf("hostaliases: %s\n",*host->h_aliases); 
  77.             break; 
  78.         } 
  79.         printf("hostaliases: %s\n",*host->h_aliases); 
  80.         host->h_aliases++; 
  81.     } 
  82. //AF_INET=2   AF_INET6=10 
  83.     switch(host->h_addrtype) 
  84.     { 
  85.         case AF_INET: 
  86.                 { 
  87.                     printf("addresstype:AF_INET\n"); 
  88.                     break;   
  89.                 } 
  90.         case AF_INET6: 
  91.                 { 
  92.                     printf("addresstype:AF_INET6\n"); 
  93.                     break;   
  94.                 } 
  95.         default: 
  96.                 { 
  97.                     printf("addresstype:other AF_XXX\n"); 
  98.                     break;                   
  99.                 }        
  100.     } 
  101.     printf("address_length:%d byte\n",host->h_addrtype); 
  102.      
  103.     while(*host->h_addr_list != NULL) 
  104.     { 
  105. //INET_ADDRSTRLEN   ,INET6_ADDRSTRLEN   两个在头文件里面定义了足够大的空间 
  106.         char buf[INET_ADDRSTRLEN]; 
  107.         memset(buf,INET_ADDRSTRLEN,0); 
  108.         inet_ntop(AF_INET, (void*)*host->h_addr_list, buf, INET_ADDRSTRLEN); 
  109.         printf("host->h_addr_list: %s\n",buf); 
  110.         host->h_addr_list++; 
  111.     } 
  112.  
  113.  
  114. void test_gethostent() 
  115.     struct hostent* host = gethostent(); 
  116.     if(host == NULL) 
  117.     { 
  118.         perror("gethostent\n"); 
  119.         exit(1); 
  120.     }    
  121.     show_hostent(host); 
  122.  
  123.  
  124.  
  125.  
  126. void test_getnetent(void) 
  127.     struct netent *net = getnetent(); 
  128.      
  129.     if(net == NULL) 
  130.     { 
  131.         perror("getnetent\n"); 
  132.         exit(1); 
  133.     }    
  134.     show_netent(net);    
  135.  
  136. void show_netent(struct netent* net) 
  137.     printf("n_name:%s\n", net->n_name); 
  138.      
  139.     while(1) 
  140.     { 
  141.         if(*net->n_aliases == NULL) 
  142.         { 
  143.             printf("n_aliases: NULL\n"); 
  144.             break; 
  145.         } 
  146.         printf("n_aliases:%s\n",*net->n_aliases); 
  147.         net->n_aliases++; 
  148.     } 
  149. // The type of the network number; always AF_INET. 
  150.     printf("n_addrtype:%d(AF_INET)\n", net->n_addrtype); 
  151.  
  152.     char buf[INET_ADDRSTRLEN]; 
  153.     memset(buf,INET_ADDRSTRLEN,0); 
  154.     inet_ntop(AF_INET, (void*)&net->n_net, buf, INET_ADDRSTRLEN); 
  155.     printf("n_net: %s\n",buf);   
  156.      
  157.  
  158. -------------------------------------------------------- 
  159. socket.h 
  160. -------------------------------------------------------- 
  161. #ifndef SOCKET_H 
  162.  
  163.  
  164. #define SOCKET_H 
  165.  #include <sys/types.h>       
  166.  #include <sys/socket.h> 
  167.  #include <stdio.h> 
  168.  #include <stdlib.h> 
  169.  #include <netdb.h> 
  170.  #include <string.h> 
  171.  #include <arpa/inet.h> 
  172. void test_gethostent(void); 
  173. void show_hostent(struct hostent*); 
  174.  
  175. void test_getnetent(void); 
  176. void show_netent(struct netent*); 
  177.  
  178.  
  179.  
  180.  
  181. #endif//end SOCKET_H 
  182.  
  183. -------------------------------------------------------- 
  184. makefile 
  185. -------------------------------------------------------- 
  186. obj =main.o socket.o 
  187.  
  188. main:$(obj) 
  189.     gcc -o main main.o socket.o 
  190. main.o:main.c  
  191.     gcc -c main.c 
  192. socket.o:socket.c 
  193.     gcc -c socket.c 
  194.  
  195. .PHONY:clean 
  196.  
  197. clean:  
  198.     rm -f main *~ *gch *.o 
  199.