-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgethostbyname.c
42 lines (36 loc) · 912 Bytes
/
gethostbyname.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// 通过域名获取IP地址
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include "error_handling.h"
int main(int arvc, char *argv[])
{
int i;
struct hostent *host;
if (arvc != 2)
{
printf("Usage: %s <domain>\n", argv[0]);
exit(1);
}
host = gethostbyname(argv[1]);
if (!host)
{
error_handling("gethostbyname() error: %s", strerror(errno));
}
printf("Official name: %s\n", host->h_name);
for (i = 0; host->h_aliases[i]; i++)
{
printf("testtest");
printf("Alias %d: %s\n", i + 1, host->h_aliases[i]);
}
printf("Address type %d\n", host->h_addrtype);
for (i = 0; host->h_addr_list[i]; i++)
{
printf("IP addr %d: %s\n", i + 1, inet_ntoa(*(struct in_addr *)host->h_addr_list[i]));
}
return 0;
}