Get ip address from hostname in C using Linux sockets

本文介绍两种方法来获取主机名对应的IP地址:一种是使用传统的gethostbyname函数,另一种是使用getaddrinfo函数。这两种方法均通过C语言实现,并提供了完整的代码示例及运行结果。

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 
9int hostname_to_ip(char *  , char *);
10 
11int 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 
32int 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
2www.google.com resolved to 74.125.235.16
3 
4$ gcc hostname_to_ip.c && ./a.out www.msn.com
5www.msn.com resolved to 207.46.140.34
6 
7$ gcc hostname_to_ip.c && ./a.out www.yahoo.com
8www.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 
9int hostname_to_ip(char *  , char *);
10 
11int 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 
32int 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
2www.google.com resolved to 74.125.235.19
3 
4$ gcc hostname_to_ip.c && ./a.out www.yahoo.com
5www.yahoo.com resolved to 72.30.2.43
6 
7$ gcc hostname_to_ip.c && ./a.out www.msn.com
8www.msn.com resolved to 207.46.140.34
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值