为什么libcurl有时会抱怨并且无法解析主机名'?

时间:2017-04-17 11:48:43

标签: curl libcurl

我编写了一个使用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();
}

这是invocer。

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;
}

是否可能由数据崩溃引起?

1 个答案:

答案 0 :(得分:6)

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

我引用Exit status section from the curl book

  

无法解析主机。给定的远程主机的地址不是   解决。无法解析给定服务器的地址。   给定的主机名是错误的,或DNS服务器是错误的   行为不端,并且在它应该或可能时不知道这个名字   甚至你运行curl的系统配置错误,所以它没有   找到/使用正确的DNS服务器。

如果对于有时有效但有时无法运行的主机名间歇性地返回,则表示您的系统已损坏,DNS服务器无法正常响应或者某种DOS预防错误调整。