博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Get ip address from hostname in C using Linux sockets
阅读量:6847 次
发布时间:2019-06-26

本文共 3271 字,大约阅读时间需要 10 分钟。

Here are 2 methods to get the ip address of a hostname :

The first method uses the traditional gethostbyname function to retrieve information about a hostname/domain name.

Code

1 #include<stdio.h> //printf
2 #include<string.h> //memset
3 #include<stdlib.h> //for exit(0);
4 #include<sys/socket.h>
5 #include<errno.h> //For errno - the error number
6 #include<netdb.h> //hostent
7 #include<arpa/inet.h>
8  
9 int hostname_to_ip(char *  , char *);
10  
11 int main(int argc , char *argv[])
12 {
13     if(argc <2)
14     {
15         printf("Please provide a hostname to resolve");
16         exit(1);
17     }
18      
19     char *hostname = argv[1];
20     char ip[100];
21      
22     hostname_to_ip(hostname , ip);
23     printf("%s resolved to %s" , hostname , ip);
24      
25     printf("\n");
26      
27 }
28 /*
29     Get ip from domain name
30  */
31  
32 int hostname_to_ip(char * hostname , char* ip)
33 {
34     struct hostent *he;
35     struct in_addr **addr_list;
36     int i;
37          
38     if ( (he = gethostbyname( hostname ) ) == NULL)
39     {
40         // get the host info
41         herror("gethostbyname");
42         return 1;
43     }
44  
45     addr_list = (struct in_addr **) he->h_addr_list;
46      
47     for(i = 0; addr_list[i] != NULL; i++)
48     {
49         //Return the first one;
50         strcpy(ip , inet_ntoa(*addr_list[i]) );
51         return 0;
52     }
53      
54     return 1;
55 }

Compile and Run

1 $ gcc hostname_to_ip.c && ./a.out www.google.com
2 www.google.com resolved to 74.125.235.16
3  
4 $ gcc hostname_to_ip.c && ./a.out www.msn.com
5 www.msn.com resolved to 207.46.140.34
6  
7 $ gcc hostname_to_ip.c && ./a.out www.yahoo.com
8 www.yahoo.com resolved to 98.137.149.56

The second method uses the getaddrinfo function to retrieve information about a hostname/domain name.

Code

1 #include<stdio.h> //printf
2 #include<string.h> //memset
3 #include<stdlib.h> //for exit(0);
4 #include<sys/socket.h>
5 #include<errno.h> //For errno - the error number
6 #include<netdb.h> //hostent
7 #include<arpa/inet.h>
8  
9 int hostname_to_ip(char *  , char *);
10  
11 int main(int argc , char *argv[])
12 {
13     if(argc <2)
14     {
15         printf("Please provide a hostname to resolve");
16         exit(1);
17     }
18      
19     char *hostname = argv[1];
20     char ip[100];
21      
22     hostname_to_ip(hostname , ip);
23     printf("%s resolved to %s" , hostname , ip);
24      
25     printf("\n");
26      
27 }
28 /*
29     Get ip from domain name
30  */
31  
32 int hostname_to_ip(char *hostname , char *ip)
33 {
34     int sockfd; 
35     struct addrinfo hints, *servinfo, *p;
36     struct sockaddr_in *h;
37     int rv;
38  
39     memset(&hints, 0, sizeof hints);
40     hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
41     hints.ai_socktype = SOCK_STREAM;
42  
43     if ( (rv = getaddrinfo( hostname , "http" , &hints , &servinfo)) != 0)
44     {
45         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
46         return 1;
47     }
48  
49     // loop through all the results and connect to the first we can
50     for(p = servinfo; p != NULL; p = p->ai_next)
51     {
52         h = (struct sockaddr_in *) p->ai_addr;
53         strcpy(ip , inet_ntoa( h->sin_addr ) );
54     }
55      
56     freeaddrinfo(servinfo); // all done with this structure
57     return 0;
58 }

Compile and Run

1 $ gcc hostname_to_ip.c && ./a.out www.google.com
2 www.google.com resolved to 74.125.235.19
3  
4 $ gcc hostname_to_ip.c && ./a.out www.yahoo.com
5 www.yahoo.com resolved to 72.30.2.43
6  
7 $ gcc hostname_to_ip.c && ./a.out www.msn.com
8 www.msn.com resolved to 207.46.140.34

转载地址:http://lzoul.baihongyu.com/

你可能感兴趣的文章
022-请你说一说PC网络故障,以及如何排除障碍
查看>>
Android开发——关于onCreate的解读
查看>>
利用百度ping工具,通过程序自动提交文章
查看>>
java代码异常,水位异常的捕获
查看>>
软件测试 -- 软件缺陷记录的5C原则
查看>>
Ch3 JS Language Basics
查看>>
Oracle学习笔记(1)
查看>>
一言不合就动手系列篇一-仿电商平台前端搜索插件(filterMore)
查看>>
Oracle Split 函数
查看>>
目标跟踪之卡尔曼滤波---理解Kalman滤波的使用预测
查看>>
Git安装和基本使用(1)
查看>>
Swoft 图片上传与处理
查看>>
BluetoothClass详解
查看>>
Centos 7安装Python3.6
查看>>
Django 学习笔记
查看>>
20172303 2017-2018-2 《程序设计与数据结构》实验三报告
查看>>
CSS自定义文件上传按钮
查看>>
排序算法概览(二)
查看>>
document对象获取例子
查看>>
java模拟http的get和post请求
查看>>