gpt4 book ai didi

curl - 为什么 libcurl 有时会提示 'Couldn' t resolve host name'?

转载 作者:行者123 更新时间:2023-12-04 09:57:40 25 4
gpt4 key购买 nike

我写了一个使用 libcurl 的多线程程序,但有时 curl 会提示它在 exec curl_easy_perform 后无法解析主机名,有时则不会。

size_t Http::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
MemoryStruct *mem = (MemoryStruct *)userp;

mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
assert(NULL != mem->memory);
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;

return realsize;
}

void Http::run(const URL &url, Http::FinishedCallback cbk)
{
CURL *handle = curl_easy_init();
if (handle)
{

MemoryStruct *chunk = new MemoryStruct;
chunk->memory = (char *)malloc(1); /* will be grown as needed by the realloc above */
chunk->size = 0; /* no data at this point */


CURLcode res;
curl_easy_setopt(handle, CURLOPT_URL, url.getUrl().c_str());
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)chunk);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(handle, CURLOPT_TIMEOUT, 10L);
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 10L);
res = curl_easy_perform(handle);

/* check for errors */
if (CURLE_OK != res)
{
chunk->status = false;
std::string errCode = std::to_string(res);
chunk->memory = (char *)realloc(chunk->memory, chunk->size + errCode.size() + 1);
memcpy(&(chunk->memory[chunk->size]), errCode.c_str(), errCode.size());
chunk->size += errCode.size();
chunk->memory[chunk->size] = 0;
}
else
{
chunk->status = true;
}

#ifdef _DEBUG
chunk->url = url;
#endif
cbk(chunk);
free(chunk);
curl_easy_cleanup(handle);
}
}


void Http::get(URL url, FinishedCallback cbk)
{
std::thread http(&Http::run, Http(), url, cbk);
http.detach();
}

Http::~Http()
{
curl_global_cleanup();
}

这是调用者。
int index = 0;
bool finish = false;
void func(Http::MemoryStruct *memo)
{
if (!memo->status)
{
#ifdef _DEBUG
LOG(INFO) << "Failure:\t" << curl_easy_strerror((CURLcode)atoi(memo->memory)) << "\n";

#endif
}
else
{
//LOG(INFO) << memo->memory << '\n';
}
finish = memo->status;
++index;
}

int main(void)
{
curl_global_init(CURL_GLOBAL_ALL);
URL::AttribMap attribMap{ { "class", "System" }, { "token", "KY0SQ3FX996PU1ZO" }, { "type", "GetConfig" } };
URL url("open.55.la", "/index.php", attribMap);
Http http;
while(index++ <=10)
{
try
{
http.get(url, func);
}
catch (std::_System_error &err)
{
LOG(INFO) << err.what();
}
}
while (true)
{
;
}
el::Loggers::flushAll();
return EXIT_SUCCESS;
}

会不会是数据崩溃造成的?

最佳答案

这是 libcurl 为 CURLE_COULDNT_RESOLVE_HOST 返回的错误消息错误代码 (6)。

我会引用 Exit status section from the curl book

Couldn't resolve host. The given remote host's address was not resolved. The address of the given server could not be resolved. Either the given host name is just wrong, or the DNS server is misbehaving and doesn't know about this name when it should or perhaps even the system you run curl on is misconfigured so that it doesn't find/use the correct DNS server.



如果这对于有时有效有时无效的主机名间歇性地返回,则表明您的系统以某种方式损坏,DNS 服务器无法正确响应,或者可能错误地调整了某种 DOS 预防。

关于curl - 为什么 libcurl 有时会提示 'Couldn' t resolve host name'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43450899/

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