首页 > Web开发 > 详细

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

时间:2019-10-22 15:28:29      阅读:107      评论:0      收藏:0      [点我收藏+]
技术分享图片
 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

 

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

原文:https://www.cnblogs.com/--just-lbk/p/11719832.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!