libcurl in multithreads
来源:互联网 发布:单管共射放大电路数据 编辑:程序博客网 时间:2024/05/19 17:56
libcurl在多线程中使用时,需要设置CURLOPT_NOSIGNAL为1,否则会引起程序crash。 但是设置了CURLOPT_NOSIGNAL之后,在域名dns解析时,设置的超时就不起作用,这样容易引起整个任务超时。此时,解决此问题,使超时设置生效的方法是:重新编译libcurl,使c-ares生效(异步解析dns),具体方法如下:1. 安装c-ares: yum install c-ares-devel2.编译libcurl:wget http://curl.haxx.se/download/curl-7.40.0.tar.gztar zxvf curl-7.40.0.tar.gz cd curl-7.40.0/./configure --enable-ares --prefix=/usr/local/curl --with-sslmake && make install编译过程中,可能会出现~/lib/version.c 110: implicit declaration of 'ares_version', 只需要在头文件包含:#include<ares_version.h> 就可以解决。
编译完成之后,应用程序重新编译即可。此时域名解析就不会卡住。具体使用实例如下:
CURL* curl; CURLcode res; long resp_code; curl = curl_easy_init(); if (!curl) { return -1; } curl_easy_setopt(curl, CURLOPT_URL, url); if (method == HTTP_METHOD_POST) curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, size); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2L); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L); /*When using multiple threads you should set the CURLOPT_NOSIGNAL(3) option to 1 for all handles. Everything will or might work fine except that timeouts are not honored during the DNS lookup- which you can work around by building libcurl with c-ares support. c-ares is a library that provides asynchronous name resolves.*/ curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L); res = curl_easy_perform(curl); if (res != CURLE_OK) { curl_easy_cleanup(curl); return -1; } /*must use parameter witch type long to get response code, otherwise will cause stack problem*/ curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &resp_code); *http_code = resp_code; curl_easy_cleanup(curl);
阅读全文
0 0
- libcurl in multithreads
- JAVA 8 In Action 读书笔记 (五) : MultiThreads
- libcurl in WebKit
- Using libcurl in VC++
- libcurl
- libcurl
- libcurl
- libcurl
- libcurl
- libcurl
- libcurl
- libcurl
- libcurl
- libcurl
- libcurl
- Send string in PUT request with libcurl in C++
- How to use libcurl in C++ with std::string
- Git hub protocol https not supported or disabled in libcurl
- vs2010构建WDK出现faltal error U1087:cannot have
- 已include包却提示未定义标识符
- scp自动交互输入密码
- Linux驱动之一线式总线(以ds18b20为例)
- excel导入mysql长数字字符串变科学计数法的解决方法
- libcurl in multithreads
- Android: Jenkins+SVN+Gradle+QQ Mojo实现项目自动化构建
- winform移动无边框窗体
- HDU--dp练习--1021--最少拦截系统
- Java反射备忘
- 【已解决】Android Studio下,gradle project sync failed 错误
- JS转义 escape()、encodeURI()、encodeURIComponent()区别详解
- Linux驱动之I2C总线
- 生成14个可重复一次的随机数