gpt4 book ai didi

c++ - Ubuntu:gethostbyaddr 返回 NULL 和 HOST_NOT_FOUND 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:01:05 24 4
gpt4 key购买 nike

我的机器是 Ubuntu Server 10.10。

我有以下代码片段:

#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char *argv[])
{
struct in_addr addr;
struct hostent *he;
/* printf ("sizeof struct in_addr: %i\n", sizeof(struct in_addr)); */
printf("in_addr: %s\n", argv[1]);
addr.s_addr = inet_addr(argv[1]);
if (addr.s_addr == 0 || addr.s_addr == -1)
{
perror("inet_addr");
return 1;
}
/* else */
he = gethostbyaddr(&addr, 4, AF_INET);
if (he == NULL)
{
perror("gethostbyaddr");
switch (h_errno)
{
case HOST_NOT_FOUND:
printf("HOST_NOT_FOUND\n");
break;
case NO_ADDRESS:
printf("NO_ADDRESS\n");
break;
/* case NO_DATA: print ("NO_DATA\n"); break; */
case NO_RECOVERY:
printf("NO_RECOVERY\n");
break;
case TRY_AGAIN:
printf("TRY_AGAIN\n");
break;
}
return 1;
}
/* else */
printf("%s\n", he->h_name);
return 0;
}

我运行它:

./main 10.50.10.252

(10.50.10.252 是我的 eth0 IP 地址)

$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:65:0d:27
inet addr:10.50.10.252 Bcast:10.50.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe65:d27/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1225069 errors:0 dropped:0 overruns:0 frame:0
TX packets:980849 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:695573851 (695.5 MB) TX bytes:658044535 (658.0 MB)
Interrupt:19 Base address:0x2000

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:783434 errors:0 dropped:0 overruns:0 frame:0
TX packets:783434 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:701737148 (701.7 MB) TX bytes:701737148 (701.7 MB)

但是输出是:

in_addr: 10.50.10.252
gethostbyaddr: Success
HOST_NOT_FOUND

我只是不知道为什么 HOST_NOT_FOUND,因为我可以在我的机器上使用互联网、ssh、scp...。

附言:cat/etc/主机

127.0.0.1       localhost
127.0.1.1 ubuntu.localdomain ubuntu

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

cat/etc/nsswitch.conf

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd: compat
group: compat
shadow: compat

hosts: files dns
networks: files

protocols: db files
services: db files
ethers: db files
rpc: db files

netgroup: nis

cat/etc/host.conf

# The "order" line is only used by old versions of the C library.
order hosts,bind
multi on

最佳答案

对于反向 DNS 查找,行为由/etc/host.conf 控制。您需要在其中指定主机。您需要在/etc/host.conf 中添加这一行:

order hosts,bind

参见 http://tldp.org/LDP/nag/node82.html .

关于c++ - Ubuntu:gethostbyaddr 返回 NULL 和 HOST_NOT_FOUND 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6837506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com