项目场景:
MVR 设备开机启动,待4G拨号成功后开始连接平台
问题描述
if ( (phe = gethostbyname(host)) == NULL )
原因分析:
gethostbyname(host) 返回NULL,无法解析域名
解决方案:
if (0 != res_init())
{
return -1;
}
if ( (phe = gethostbyname(host)) == NULL )
{
return -2;
}
每次调用 gethostbyname 之前,先调用 res_init 函数。关于 res_init 函数的说明,摘要 man 手册的说明:
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).
If no server is given, the local host is tried. If no domain is given, that associated with the local host is used. It can be overridden with the
environment variable LOCALDOMAIN. res_init() is normally executed by the first call to one of the other functions.
res_init()函数读取配置文件(参见resolv.conf(5))以获得默认域名、搜索顺序和名称服务器地址。
如果没有提供服务器,则尝试本地主机。如果没有给出域,则使用与本地主机关联的域。它可以被重写
环境变量LOCALDOMAIN。Res_init()通常在第一次调用其他函数时执行。