我正在尝试使用curl_multi进行2个并行连接:
CURL *http_handle;
CURL *http_handle2;
CURLM *multi_handle;
int still_running; /* keep number of running handles */
http_handle = curl_easy_init();
http_handle2 = curl_easy_init();
/* set options */
curl_easy_setopt(http_handle, CURLOPT_URL, "http://216.58.208.46");
/* set options */
curl_easy_setopt(http_handle2, CURLOPT_URL, "http://213.180.204.62");
curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(http_handle2, CURLOPT_VERBOSE, 1L);
/* init a multi stack */
multi_handle = curl_multi_init();
/* add the individual transfers */
curl_multi_add_handle(multi_handle, http_handle);
curl_multi_add_handle(multi_handle, http_handle2);
/* we start some action by calling perform right away */
curl_multi_perform(multi_handle, &still_running);
while(still_running);
curl_multi_cleanup(multi_handle);
curl_easy_cleanup(http_handle);
curl_easy_cleanup(http_handle2);
return 0;
并获取控制台输出:
- 重建网址为:http://216.58.208.46/
- 在 DNS 缓存中未找到主机名
- 正在尝试 216.58.208.46...
- 重建网址为:http://213.180.204.62/
- 在 DNS 缓存中未找到主机名
- 正在尝试 213.180.204.62...
如果我使用curl_easy_perform但我不使用curl_multi_perform,一切都会完美运行,那么libcurl中是否存在错误或者我做错了什么?我的 libcurl 版本是 7.37.1
最佳答案
你似乎误解了 curl_multi_perform作品。它只执行一小部分传输然后返回,您需要不断调用它直到所有传输完成。 (不在繁忙循环中,您还应该在再次调用它之前等待“action”。)
显示使用多接口(interface)完成的两个并行传输的示例代码是 multi-double curl 网站上的示例。
有关在 DNS 缓存中找不到的文本只是垃圾,将在未来版本中删除,而“重建”文本只是告诉您 libcurl 如何自动为您修复 URL,并且它会在以后使用该固定版本。 “尝试”部分是 libcurl 启动与主机的连接,但由于您再也没有调用它,因此它无法完成其工作!
关于c++ - 在 DNS 缓存中未找到 Libcurl 主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28065002/