ping 成功但 gethostbyname() returns 为空
ping successfully but gethostbyname() returns null
我正在为设备编程 运行ning Linux OS.
设备 运行s ping
命令 youtube.com
成功。但是,在源代码中,如果我调用 gethostbyname("youtube.com")
,函数 returns NULL
.
更新日志
我的代码如下所示(这只是描述我的问题的简短伪代码,不是完整代码)
void my_func()
{
struct hostent *hostEntry;
hostEntry = gethostbyname("youtube.com");
if (hostEntry) {
// gethostbyname() resolve host name successfully, no need to care
} else {
printf("Fail to get host, herrno=%d, strErr=%s\n", h_errno, hstrerror(h_errno));
}
}
而设备在gethostbyname()
returnsNULL
时的输出是
Fail to get host, herrno=1, strErr=Unknown host
这是我退出程序时设备的输出 运行 ping youtube.com
$ ping youtube.com
PING youtube.com (64.233.189.91): 56 data bytes
64 bytes from 64.233.189.91: seq=0 ttl=55 time=57.824 ms
64 bytes from 64.233.189.91: seq=1 ttl=55 time=56.306 ms
64 bytes from 64.233.189.91: seq=2 ttl=55 time=56.790 ms
64 bytes from 64.233.189.91: seq=3 ttl=55 time=56.831 ms
64 bytes from 64.233.189.91: seq=4 ttl=55 time=56.417 ms
我现在应该检查什么?
我添加 res_init()
来解决这个问题,幸运的是,它有效。
#include <resolv.h>
void my_func()
{
struct hostent *hostEntry;
hostEntry = gethostbyname("youtube.com");
if (hostEntry) {
res_init();
hostEntry = gethostbyname("youtube.com");
}
if (hostEntry) {
// gethostbyname() resolve host name successfully, no need to care
} else {
printf("Fail to get host, herrno=%d, strErr=%s\n", h_errno, hstrerror(h_errno));
}
}
这可能是函数帮助的原因
The res_init() function reads the configuration files (see resolv.conf(5)) to get the default domain name, search order and name server address(es).
我正在为设备编程 运行ning Linux OS.
设备 运行s ping
命令 youtube.com
成功。但是,在源代码中,如果我调用 gethostbyname("youtube.com")
,函数 returns NULL
.
更新日志
我的代码如下所示(这只是描述我的问题的简短伪代码,不是完整代码)
void my_func()
{
struct hostent *hostEntry;
hostEntry = gethostbyname("youtube.com");
if (hostEntry) {
// gethostbyname() resolve host name successfully, no need to care
} else {
printf("Fail to get host, herrno=%d, strErr=%s\n", h_errno, hstrerror(h_errno));
}
}
而设备在gethostbyname()
returnsNULL
时的输出是
Fail to get host, herrno=1, strErr=Unknown host
这是我退出程序时设备的输出 运行 ping youtube.com
$ ping youtube.com
PING youtube.com (64.233.189.91): 56 data bytes
64 bytes from 64.233.189.91: seq=0 ttl=55 time=57.824 ms
64 bytes from 64.233.189.91: seq=1 ttl=55 time=56.306 ms
64 bytes from 64.233.189.91: seq=2 ttl=55 time=56.790 ms
64 bytes from 64.233.189.91: seq=3 ttl=55 time=56.831 ms
64 bytes from 64.233.189.91: seq=4 ttl=55 time=56.417 ms
我现在应该检查什么?
我添加 res_init()
来解决这个问题,幸运的是,它有效。
#include <resolv.h>
void my_func()
{
struct hostent *hostEntry;
hostEntry = gethostbyname("youtube.com");
if (hostEntry) {
res_init();
hostEntry = gethostbyname("youtube.com");
}
if (hostEntry) {
// gethostbyname() resolve host name successfully, no need to care
} else {
printf("Fail to get host, herrno=%d, strErr=%s\n", h_errno, hstrerror(h_errno));
}
}
这可能是函数帮助的原因
The res_init() function reads the configuration files (see resolv.conf(5)) to get the default domain name, search order and name server address(es).