C++ gethostbyaddr错误未知主机 #包括 #包括 #包括 #包括 #包括 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(主机名!=NULL&&hostnames[0].h\u name!=NULL){ printf(“%s\n”,主机名[0].h\u名称); 返回0; }否则{ herror(“gethostbyaddr”); 返回1; } }

C++ gethostbyaddr错误未知主机 #包括 #包括 #包括 #包括 #包括 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(主机名!=NULL&&hostnames[0].h\u name!=NULL){ printf(“%s\n”,主机名[0].h\u名称); 返回0; }否则{ herror(“gethostbyaddr”); 返回1; } },c++,c,sockets,networking,gethostbyaddr,C++,C,Sockets,Networking,Gethostbyaddr,它返回“gethostbyaddr:未知主机”。我尝试了不同的IP。怎么了 有人能帮我吗? 谢谢你的代码没有为我编译。我在_addr和hostent中发现错误。但是,如果我将它们的声明分别更改为struct in_addr和struct hostent,则它编译时不会发出警告,并在运行时发出以下输出: #include <stdio.h> #include <netdb.h> #include <sys/socket.h> #include <netin

它返回“gethostbyaddr:未知主机”。我尝试了不同的IP。怎么了

有人能帮我吗?
谢谢你的代码没有为我编译。我在_addr和
hostent
中发现错误。但是,如果我将它们的声明分别更改为
struct in_addr
struct hostent
,则它编译时不会发出警告,并在运行时发出以下输出:

#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;
    }
}
这似乎是正确的


如果它按原样编译,那么您可能正在使用不同的操作系统。我尝试了Mac OS和Linux,结果相同。< / P>这个代码来自C++代码,不需要“Stutt”关键字。我使用的是Mac OS X 10.6,但它不工作(loooong等待,然后出现错误)“…loooong等待…”:这听起来像是DNS超时@是的,这是一个暂停。我在Fedora 17下的另一台计算机上测试了这个程序,它运行正常。所以代码是好的。。。
lb-in-f94.1e100.net