http://linuxcstudy.5d6d.com/thread-23-1-1.html
论坛上的一个问题尚未解决,有会的请帮帮吗!
取得域名的IP
编写一个简易的取得域名的IP代码(内容绝对可靠)
dewboy发表于 12 小时前 | 只看该作者
#include "stdio.h"
#include "sys/socket.h"
#include "netdb.h"
int main(int argc,char *argv[])
{
struct hostent *host;
char hostname[]="www.163.com";
char hostname2[]="www.xiaoxiaowanruo.com";
struct in_addr in;
struct sockaddr_in addr_in;
extern int h_errno;
if((host=gethostbyname(hostname))!=NULL)
{
memcpy(&addr_in.sin_addr.s_addr,host->h_addr,4);
in.s_addr=addr_in.sin_addr.s_addr;
printf("Domain name:%s \n",hostname);
printf("IP length: %d \n",host->h_length);
printf("Type: %d \n ",host->h_addrtype);
printf("IP :%s \n",inet_ntoa(in));
}
else
{
printf("Domain name: %s \n",hostname);
printf("error: %d \n ",h_errno);
printf("%s\n",hstrerror(h_errno));
}
if((host=gethostbyname(hostname2))!=NULL)
{
memcpy(&addr_in.sin_addr.s_addr,host->h_addr,4);
in.s_addr=addr_in.sin_addr.s_addr;
printf("Domain name:%s \n",hostname2);
printf("IP length: %d \n",host->h_length);
printf("Type: %d \n ",host->h_addrtype);
printf("IP :%s \n",inet_ntoa(in));
}
else
{
printf("Domain name: %s \n",hostname2);
printf("error: %d \n ",h_errno);
printf("%s\n",hstrerror(h_errno));
}
}
只看该作者 仍有疑问,在解析www.xiaoxiaowanruo.com时,返回的ip地址为125.211.213.133 。如果把www.xiaoxiaowanruo.com 改为
www.liumingyue.com,结果仍未变,这是为什么?
18687533 于 2009-05-18 17:38:59发表:
希望能看到基础实用的教程{:2_103:}
ffsedan 于 2009-05-18 17:37:53发表:
高深
lpscoolio 于 2009-05-18 16:54:25发表:
学习
414716676 于 2009-05-18 01:15:46发表:
学习了!!!!!!!