3
我有以下腳本:的gethostbyname失敗與gethostbyaddr主機名,從而成功
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
const char *ip="190.162.1.2";
int main(int argc, const char * argv[])
{
in_addr host_addr;
hostent *addr=0;
hostent *host=0;
inet_pton(AF_INET, ip, &host_addr);
addr=gethostbyaddr(&host_addr, sizeof(host_addr), AF_INET);
printf("gethostbyaddr(%s):\n",ip);
if(!addr)
herror("Unable to resolve host by address");
else {
printf(" OK\n");
host=gethostbyname(addr->h_name); //use the hostname we got previously
printf("gethostbyname(%s):\n",addr->h_name);
if(!host){
herror(" Unable to resolve host by name"); //gets here, why?
return 0;
} else {
printf(" IP addresses of %s: \n",addr->h_name);
in_addr **addr_list;
addr_list = (in_addr **)host->h_addr_list;
for(int i = 0; addr_list[i] != NULL; i++) {
printf("%s \n", inet_ntoa(*addr_list[i]));
}
}
}
return 0;
}
我不知道是爲什麼的gethostbyname與主機從之前成功gethostbyaddr失敗。誰能解釋我爲什麼?
進展:
gethostbyaddr(190.162.1.2):
OK
gethostbyname(pc-2-1-162-190.cm.vtr.net):
Unable to resolve host by name: Unknown host
但它與像173.194.34.129其他IP地址(google.com)等作品
ping pc-2-1-162-190.cm.vtr.net =>未知主機+ nah,在我的本地配置中不會出現錯誤,當我嘗試在任何在線狀態下查看主機時服務,結果是一樣的... http://www.dnswatch.info,http://www.hcidata.info/host2ip.htm,http://hostnametoip.com,... – jakubinf
Ping不是正確的工具,請使用nslookup。 nslookup 190.162.1.2的作品,nslookup pc-2-1-162-190.cm.vtr.net沒有。所以你是正確的,pc-2-1-162-190.cm.vtr.net不在DNS中。 – Ciclamino
所以你已經證明你的程序正在做正確的事情...... – Ciclamino