Get ip address from hostname in C using Linux sockets

简介:

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
目录
相关文章
|
3月前
|
域名解析 网络协议 Linux
入职必会-开发环境搭建38-Linux常用操作-Linux设置IP
目前安装的Linux操作系统默认是动态获取IP地址,IP地址可能会发生变动,需要把IP地址设置为静态的。
入职必会-开发环境搭建38-Linux常用操作-Linux设置IP
|
12天前
|
Web App开发 资源调度 网络协议
Linux系统之部署IP工具箱MyIP
【10月更文挑战第5天】使用Docker部署Radicale日历和联系人应用Linux系统之部署IP工具箱MyIP
43 1
Linux系统之部署IP工具箱MyIP
|
2月前
|
存储 Linux Shell
在Linux中,如何使用脚本,实现判断 192.168.1.0/24 网络里,当前在线的 IP 有哪些?能ping 通则 认为在线。
在Linux中,如何使用脚本,实现判断 192.168.1.0/24 网络里,当前在线的 IP 有哪些?能ping 通则 认为在线。
|
2月前
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
2月前
|
机器学习/深度学习 Ubuntu Linux
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
|
2月前
|
网络协议 Linux 网络安全
在Linux中,如何将本地 80 端口的请求转发到 8080 端口?当前主机 IP 为10.0.0.104。
在Linux中,如何将本地 80 端口的请求转发到 8080 端口?当前主机 IP 为10.0.0.104。
|
2月前
|
网络协议 Linux
在Linux中,如何改IP、主机名、DNS?
在Linux中,如何改IP、主机名、DNS?
|
2月前
|
网络协议 Ubuntu Linux
在Linux中,如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.16.1,其中本地网卡eth0。
在Linux中,如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.16.1,其中本地网卡eth0。
|
2月前
|
监控 网络协议 Linux
在Linux中,如何查看当前系统每个 IP 的连接数?
在Linux中,如何查看当前系统每个 IP 的连接数?
|
2月前
|
域名解析 缓存 负载均衡
在Linux中,自定义解析域名的时候,可以编辑哪个⽂件?是否可以⼀个ip对应多个域名?是否⼀个域名对应多个ip?
在Linux中,自定义解析域名的时候,可以编辑哪个⽂件?是否可以⼀个ip对应多个域名?是否⼀个域名对应多个ip?