gpt4 book ai didi

c++ - Linux c++ : getaddrinfo failed with EAI_AGAIN, curl 无法解析主机

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

我通过 ppp 打开 GSM 连接并尝试使用 curl 从 URL 下载文件。

对于少数设备,curlgetaddrinfo 导致 EAI_AGAIN 出现 CURLE_COULDNT_RESOLVE_HOST 错误。

route 中,我的 ppp0 接口(interface)注册正确。 resolv.conf 包含 google DNS 并且在终端中手动执行 nslookup 是有效的。

我不知道为什么它不起作用。我唯一知道的是:IPv6 是不可能的。

这是我的getaddrinfo

代码
int checkINet(std::string address)
{
struct addrinfo hints, *servinfo, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
char ip_addr[INET6_ADDRSTRLEN];

int s = getaddrinfo(address.c_str(), NULL, &hints, &servinfo);

if (s != 0)
{
std::cout << "No connection possible: " << address << "\n";
std::cout << "getaddrinfo: " << s << " " << gai_strerror(s) << "\n";
return -1;
}
else
{
std::cout << "Conection successful: " << address << "\n";

for (p = servinfo; p != NULL; p = p->ai_next)
{
void *addr;
if (p->ai_family == AF_INET)
{
struct sockaddr_in *ipv4 =(struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
}
else
{
struct sockaddr_in6 *ipv6 =(struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
}

inet_ntop(p->ai_family, addr, ip_addr, sizeof ip_addr);
std::cout << addr << "\n";
}

}
freeaddrinfo(servinfo);
return 0;
}

对于 curl :

   CURL *curl;
FILE *fp;
CURLcode res;

// initialise a connection to the server
curl = curl_easy_init();
if (curl)
{
fp = fopen(UPDATE_ZIP, "wb");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);

if (CURLE_OK != res)
{
std::out << "curl failed with: " << res << "\n";
return res;
}
return 0;
}
return -1;

最佳答案

ephemient tipp 是对的。更改了 /etc/nsswitch.con 中的顺序主机:文件 dns主机:dns 文件

关于c++ - Linux c++ : getaddrinfo failed with EAI_AGAIN, curl 无法解析主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47077846/

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