通过调用resolvParamsSet已设置好dns server address并生效,通过ping www.sohu.com 已能显示其对应的ip并成功。现在想验证一下通过调用resolvGetHostByName是否能由域名获得ip。我是这样做的,只是简单试一下能否解析出www.sohu.com的ip:
struct hostent *GetIP;
char pbuf[1024];
GetIP=resolvGetHostByName("www.sohu.com",line,1024);
printf("name=%s\n",GetIP->h_name);
printf("alias=%s\n",*(GetIP->h_aliases));
printf("type=%d\n",GetIP->h_addrtype);
printf("length=%d\n",GetIP->h_length);
printf("addr=%s\n",GetIP->h_addr_list[0]);
printf("ttl=%d",GetIP->h_ttl);
显示的结果确是:
name=cachecernet.sohu.com
alias=www.sohu.com
type=2
length=4
addr=Þ˜ŠcachÞ˜‹cachÞ˜‰net.sohu.com
ttl=100
因为resolvGetHostByName将返回hostent structure。而hostent structure是如下定义的:
struct hostent
{
char * h_name; /* official name of host */
char ** h_aliases; /* alias list */
int h_addrtype; /* address type */
int h_length; /* length of address */
char ** h_addr_list; /* list of addresses from name server */
unsigned int h_ttl; /* Time to Live in Seconds for this entry */
}
我认为h_addr_list就是域名对应的ip地址,可是打印出来却完全不对。DNS确实是设置成功了的,可是调用resolvGetHostByName却无法得到域名对应的ip.
请高手指点,谢谢!