3
성공이 gethostbyaddr에서 호스트 이름, 실패 :gethostbyname까지도 내가 스크립트 다음 한
#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;
}
그리고 왜 이전에 성공적으로 gethostbyaddr를에서 호스트 이름으로 실패으로 gethostbyname한다는 것을 생각해 본다. 아무도 이유를 설명 할 수 있을까요?
진행 :
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 => 알 수없는 호스트 + 아니, 내 로컬 설정에서 오류가되지 않습니다. 호스트를 온라인으로 보려고 할 때 서비스, 결과는 동일합니다 ... http://www.dnswatch.info, http://www.hcidata.info/host2ip.htm, http://hostnametoip.com, ... – jakubinf
핑은 맞지 않습니다. 도구를 사용하여 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