获取网站IP地址(Linux,C)

 1 #include <netdb.h>
 2 #include <stdio.h>
 3 #include <unistd.h>
 4 #include <stdlib.h>
 5 #include <arpa/inet.h>
 6 #include <netdb.h>
 7 
 8 void error_handling(char *msg);
 9 
10 int main(int argc,char **argv)
11 {
12     int i;
13 
14     struct hostent *host;
15     if (argc != 2) {
16         printf("Usage :%s <addr>\n",argv[0]);
17         exit(1);
18     }
19 
20     host = gethostbyname(argv[1]);
21     if (!host) {
22         error_handling("gethost err");
23     }
24 
25     for (i = 0;host->h_aliases[i];i++) {
26         printf("alias : %d <%s>\n",i,host->h_aliases[i]);
27     }
28     for (i = 0;host->h_addr_list[i];i++) {
29         printf("ip : %d <%s>\n",i,inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
30     }
31 
32     return 0;
33 }
34 
35 
36 void error_handling(char *msg)
37 {
38     fputs(msg,stderr);
39     fputc('\n',stderr);
40     exit(1);
41 }
View Code

 

posted @ 2019-10-22 14:58  NorseLZJ  阅读(417)  评论(0编辑  收藏  举报