- 论坛徽章:
- 0
|
各位大侠:
我碰到一个问题,比如我有一个网址的IP地址,我用gethostbyaddr()去获取他的域名。为什么返回的都是一些奇怪的地址,不是我想要的地址。
例如:外部IP为211.144.92.240。 我想得到他的hostname是www.sina.com.cn,可是执行后却是reserve.cableplus.com.cn
请问如何才能正确获得远端主机的域名呢?- #include <sys/socket.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <sys/socket.h>
- int main ()
- {
- struct in_addr addr;
- struct hostent *hostname;
- addr.s_addr = inet_addr("211.144.92.240");
- printf("loop ip:%s\n", inet_ntoa(addr));
- hostname = gethostbyaddr((char *)&addr, 4, AF_INET);
- if (hostname == NULL){
- printf("error, ");
- return -1;
- }
- printf("h_name:%s\n", hostname->h_name);
- printf("h_alia:%s\n", hostname->h_aliases[0]);
- printf("h_ip:%s\n", inet_ntoa(*((struct in_addr *)hostname->h_addr_list[0])));
- return 0;
- }
复制代码 |
|