获取网站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>
",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>
",i,host->h_aliases[i]);
27     }
28     for (i = 0;host->h_addr_list[i];i++) {
29         printf("ip : %d <%s>
",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('
',stderr);
40     exit(1);
41 }
View Code
原文地址:https://www.cnblogs.com/--just-lbk/p/11719832.html