Linux 下 gethostbyname 解析域名失败,错误码为 “11, Resource temporarily unavailable“

项目场景:

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()通常在第一次调用其他函数时执行。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值