最近学习网络知识,看了下dns相关的知识,以下代码留给自己以后查看。(运行环境xcode)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct hostent *h;
if ((h=gethostbyname("www.baidu.com")) == NULL)
{
herror("gethostbyname");
exit(1);
}
printf("Host name : %s\n", h->h_name);
printf("IP Address : %s\n",inet_ntoa(*((struct in_addr *)h->h_addr)));
return 0;
}
输入结果
//Host name : www.a.shifen.com
//IP Address : 119.75.218.77