Bootstrap

Linux域名解析得到ip地址

Linux下域名解析得到ip地址代码如下:
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
	struct hostent *p;
	int i;

	if (argc < 2)
		return -1;

	p = gethostbyname(argv[1]);
	printf("hostname %s\n", p->h_name);
	printf("address ");
	for (i = 0; p->h_addr_list[i]; i++) {
		printf("%s ", inet_ntoa(*(struct in_addr *)p->h_addr_list[i]));
	}
	printf("\n");

	return 0;
}
;