#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main () {
in_addr ip;
ip.s_addr = inet_addr("173.194.71.94"); // www.google.fr IP
hostent* hostnames = gethostbyaddr(&ip, sizeof(ip), AF_INET);
if (hostnames != NULL && hostnames[0].h_name != NULL) {
printf("%s\n", hostnames[0].h_name);
return 0;
} else {
herror("gethostbyaddr");
return 1;
}
}它返回"gethostbyaddr:未知主机“。我尝试了不同的IP地址。出什么事了?
有人能帮我吗?谢谢
发布于 2012-09-09 01:50:43
你的代码不能为我编译。我在in_addr和hostent上遇到错误。但是,如果我将它们的声明分别更改为struct in_addr和struct hostent,它在运行时编译时没有任何警告,并发出以下输出:
lb-in-f94.1e100.net这似乎是对的。
如果它按原样为您编译,那么您可能正在使用不同的操作系统。我尝试了MacOS和Linux,得到了相同的结果。
https://stackoverflow.com/questions/12332906
复制相似问题