【问题标题】:gethostbyaddr error Unknown hostgethostbyaddr错误未知主机
【发布时间】:2012-09-02 05:03:48
【问题描述】:
#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。怎么了?

有人可以帮助我吗? 谢谢

【问题讨论】:

    标签: c++ c sockets networking gethostbyaddr


    【解决方案1】:

    您的代码无法为我编译。我在in_addrhostent 上遇到错误。但是,如果我将它们的声明分别更改为 struct in_addrstruct hostent,它将在没有警告的情况下编译并在运行时发出以下输出:

    lb-in-f94.1e100.net
    

    这似乎是正确的。

    如果它为您按原样编译,那么您可能使用的是不同的操作系统。我尝试了 MacOS 和 Linux,结果相同。

    【讨论】:

    • 此代码来自不需要“struct”关键字的 C++ 代码。我在 Mac OS X 10.6 上,但它不起作用(等待很长时间,然后出现错误)
    • "... looooong wait ...":这听起来像是 DNS 超时。 @FélixFaisant
    • 是的,这是一个超时。我在 Fedora 17 下的另一台计算机上测试了这个 prog,它可以工作。所以代码没问题...
    猜你喜欢
    • 2015-08-23
    • 2012-04-17
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多