C 中的 gethostbyname
我不知道如何用 C 语言编写应用程序,但我需要一个小程序来执行以下操作:
lh = gethostbyname("localhost");
output = lh->h_name;
打印输出变量。
上面的代码在 PHP MongoDB 数据库驱动程序中用于获取计算机的主机名(主机名是生成唯一 ID 的输入的一部分)。我怀疑这是否会返回主机名,所以我想要一些证据。
任何代码示例都会非常有帮助。
快乐的一天。
I don't know how to write applications in C, but I need a tiny program that does:
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.
Any code examples would be most helpful.
Happy day.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管有时可能有效,但这并不是一种非常可靠的确定主机名的方法。 (它返回的内容取决于
/etc/hosts
的设置方式)。如果你有这样一行:...那么它将返回“foobar”。如果你有相反的方式(这也很常见),那么它只会返回“localhost”。更可靠的方法是使用 gethostname() 函数:
It is not a very reliable way of determining the hostname, though it may sometimes work. (what it returns depends on how
/etc/hosts
is set up). If you have a line like:...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:在 C/UNIX 中,等效的内容类似于:
以及它有效的证明:
但是你自己尝试一下。如果你有正确的环境,应该没问题。
In C/UNIX, the equivalent would be something like:
and the proof that it works:
But try it yourself. Provided you have the correct environment, it should be fine.
怎么了?
what is wrong?