C语言 C 中的 gethostbyname

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2865583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:28:39  来源:igfitidea点击:

gethostbyname in C

cgethostbyname

提问by Matic

I don't know how to write applications in C, but I need a tiny program that does:

我不知道如何用 C 编写应用程序,但我需要一个可以执行以下操作的小程序:

lh = gethostbyname("localhost");
output = lh->h_name;

output variable is to be printed.

要打印输出变量。

The above code is used in PHP MongoDB database driver to get the hostname of the computer (hostname is part of an input to generate an unique ID). I'm skeptical that this will return the hostname, so I'd like some proof.

以上代码用于 PHP MongoDB 数据库驱动程序,用于获取计算机的主机名(主机名是生成唯一 ID 的输入的一部分)。我怀疑这会返回主机名,所以我想要一些证据。

Any code examples would be most helpful.

任何代码示例都将是最有帮助的。

Happy day.

愉快的一天。

回答by caf

#include <stdio.h>
#include <netdb.h>


int main(int argc, char *argv[])
{
    struct hostent *lh = gethostbyname("localhost");

    if (lh)
        puts(lh->h_name);
    else
        herror("gethostbyname");

    return 0;
}

It is not a very reliable way of determining the hostname, though it may sometimes work. (what it returns depends on how /etc/hostsis set up). If you have a line like:

这不是确定主机名的非常可靠的方法,尽管它有时可能有效。(它返回的内容取决于/etc/hosts设置方式)。如果你有这样一行:

127.0.0.1    foobar    localhost

...then it will return "foobar". If you have it the other way around though, which is also common, then it will just return "localhost". A more reliable way is to use the gethostname()function:

...然后它会返回“foobar”。如果您反过来使用它,这也很常见,那么它只会返回“localhost”。更可靠的方法是使用该gethostname()函数:

#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main(int argc, char *argv[])
{
    char hostname[HOST_NAME_MAX + 1];

    hostname[HOST_NAME_MAX] = 0;
    if (gethostname(hostname, HOST_NAME_MAX) == 0)
        puts(hostname);
    else
        perror("gethostname");

    return 0;
}

回答by paxdiablo

In C/UNIX, the equivalent would be something like:

在 C/UNIX 中,等效项类似于:

#include <stdio.h>
#include <netdb.h>

int main (int argc, char *argv[]) {
    struct hostent *hstnm;
    if (argc != 2) {
        fprintf(stderr, "usage: %s hostname\n", argv[0]);
        return 1;
    }
    hstnm = gethostbyname (argv[1]);
    if (!hstnm)
        return 1;
    printf ("Name: %s\n", hstnm->h_name);
    return 0;
}

and the proof that it works:

以及它有效的证明:

$ hstnm localhost
Name: demon-a21pht

But try it yourself. Provided you have the correct environment, it should be fine.

但是你自己试试。只要你有正确的环境,它应该没问题。

回答by Andrey

what is wrong?

怎么了?

h_name

The official name of the host (PC). If using the DNS or similar resolution system, it is the Fully Qualified Domain Name (FQDN) that caused the server to return a reply. If using a local hosts file, it is the first entry after the IPv4 address.

h_name

主机 (PC) 的正式名称。如果使用 DNS 或类似的解析系统,则是完全限定域名 (FQDN) 导致服务器返回回复。如果使用本地主机文件,则它是 IPv4 地址之后的第一个条目。