curl with c_ares 编译

最近做了一个多线程下HTTP get请求,莫名其妙的异常,不是无法连接主机,就是崩溃。官方给的是curl是线程安全的,但其实用的时候就感觉很多问题,官方给的方案是 带c-ares 的curl。

1 下载curl  URL:https://curl.haxx.se

   解压 ,在解压目录下curl-7.47.1\winbuild 打开BUILD.WINDOWS.txt  看下文档里面的说明,然后看下 MakeFileBuild.vc 从这个文件里 大概能了解到基本要关联的库

   

   命令:(BUILD.WINDOWS.txt 里面的)

nmake /f Makefile.vc mode=<static or dll> <options>

where <options> is one or many of:

  VC=<6,7,8,9,10,11,12,14>     - VC versions

  WITH_DEVEL=<path>            - Paths for the development files (SSL, zlib, etc.)

                                 Defaults to sibbling directory deps: ../deps

                                 Libraries can be fetched at http://windows.php.net/downloads/php-sdk/deps/

                                 Uncompress them into the deps folder.

  WITH_SSL=<dll or static>     - Enable OpenSSL support, DLL or static

  WITH_CARES=<dll or static>   - Enable c-ares support, DLL or static

  WITH_ZLIB=<dll or static>    - Enable zlib support, DLL or static

  WITH_SSH2=<dll or static>    - Enable libSSH2 support, DLL or static

  ENABLE_SSPI=<yes or no>      - Enable SSPI support, defaults to yes

  ENABLE_IPV6=<yes or no>      - Enable IPv6, defaults to yes

  ENABLE_IDN=<yes or no>       - Enable use of Windows IDN APIs, defaults to yes

                                 Requires Windows Vista or later, or installation from:

                                 https://www.microsoft.com/downloads/details.aspx?FamilyID=AD6158D7-DDBA-416A-9109-07607425A815

  ENABLE_WINSSL=<yes or no>    - Enable native Windows SSL support, defaults to yes

  GEN_PDB=<yes or no>          - Generate Program Database (debug symbols for release build)

  DEBUG=<yes or no>            - Debug builds

  MACHINE=<x86 or x64>         - Target architecture (default is x86)

我想要 WITH_SSL  WITH_CARES WITH_ZLIB 编译进去

2 下载c-ares    URL:http://c-ares.haxx.se/

相对路径:c-ares-1.10.0\vc下有工程文件,用vs2008打开就可以,编译一下

3 下载openssl URL:https://www.openssl.org/

       自己编译

4 下载zlib  自己编译

上面工作 准备好了就可以了

编译curl 

  1 还是看build.windows.txt文件:如下:

   If you wish to support zlib, openssl, c-ares, ssh2, you will have to download
   them separately and copy them to the deps directory as shown below:
   
   somedirectory\
    |_curl-src
    | |_winbuild
    |
    |_deps
      |_ lib
      |_ include
      |_ bin

这是目录结构,你把c-ares openssl zlib的相关头文件 lib dll放到相应的文件就可以了

2 修改了MakefileBuild.vc 

找到SSL_LIBS 在后面添加cares.lib

3 修改curl-7.26.0\lib\config-win32.h

找到 如下 看注释 默认是开启线程异步DNS查找,所以修改成:
/* ---------------------------------------------------------------- */
/*                       DNS RESOLVER SPECIALTY                     */
/* ---------------------------------------------------------------- */
/*
 * Undefine both USE_ARES and USE_THREADS_WIN32 for synchronous DNS.
 */
/* Define to enable c-ares asynchronous DNS lookups. */
#define USE_ARES 1   //这里注释去掉

/* Default define to enable threaded asynchronous DNS lookups. */
#if !defined(USE_SYNC_DNS) && !defined(USE_ARES) && \
    !defined(USE_THREADS_WIN32)
/*#  define USE_THREADS_WIN32 1*/   //可注可不注
#endif
#if defined(USE_ARES) && defined(USE_THREADS_WIN32)
#  error "Only one DNS lookup specialty may be defined at most"
#endif

4 打开vs2008的命令行:

  输入:nmake -f Makefile.vc mode=dll DEBUG=no  VC=9 WITH_SSL=dll WITH_ZLIB=dll USE_IDN=no USE_SSSPI=yes USE_IPV6=yes GEN_PDB=yes

 注:如果使用USE_IDN ,文档里说:

 ENABLE_IDN=<yes or no>       - Enable use of Windows IDN APIs, defaults to yes
                                 Requires Windows Vista or later, or installation from:
                                 https://www.microsoft.com/downloads/details.aspx?FamilyID=AD6158D7-DDBA-416A-9109-07607425A815

需要安装这个。不然编译时编不过。


截止目前没有做过测试,还不知道能用不,但会有问题,因为openssl.要线程安全必须设置回调,可以参考
openssl工程下openssl\crypto\threads\mttest.c

参考:

https://www.openssl.org/docs/man1.0.1/crypto/CRYPTO_set_locking_callback.html

https://curl.haxx.se/libcurl/c/threaded-ssl.html

https://curl.haxx.se/libcurl/c/opensslthreadlock.html


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
感谢您提供代码。根据代码的格式和错误提示,我猜测您可能遇到了以下问题: 1. 头文件和命名空间 您需要在代码开头添加头文件和命名空间的声明,如下所示: ```c++ #include <iostream> #include <fstream> #include <string> #include <curl/curl.h> using namespace std; ``` 2. 字符串拼接 在设置FTP服务器地址和端口号时,您需要使用字符串拼接符号“+”连接两个字符串,如下所示: ```c++ curl_easy_setopt(curl, CURLOPT_URL, (server + ":" + to_string(port)).c_str()); ``` 3. 数据类型转换 在设置FTP服务器地址和端口号时,您需要使用C++11标准库中的to_string函数将整数类型的端口号转换为字符串类型,如下所示: ```c++ curl_easy_setopt(curl, CURLOPT_URL, (server + ":" + to_string(port)).c_str()); ``` 4. FTP请求选项 设置FTP请求选项时,您需要使用CURL的常量值,如下所示: ```c++ curl_easy_setopt(curl, CURLOPT_USERAGENT, "FTP Client"); curl_easy_setopt(curl, CURLOPT_USERPWD, (username + ":" + password).c_str()); curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); curl_easy_setopt(curl, CURLOPT_URL, (server + ":" + to_string(port)).c_str()); curl_easy_setopt(curl, CURLOPT_FTPPORT, "-"); // 使用被动模式连接FTP服务器 curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L); // 创建不存在的目录 ``` 5. CURLcode类型的错误码 在执行FTP请求后,您需要检查CURLcode类型的错误码,如下所示: ```c++ CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { cerr << "Error: " << curl_easy_strerror(res) << endl; return 1; } ``` 根据以上问题,您可以修改您的代码,如下所示: ```c++ #include <iostream> #include <fstream> #include <string> #include <curl/curl.h> using namespace std; int main(int argc, char* argv[]) { // 初始化CURLcurl_global_init(CURL_GLOBAL_ALL); // 创建CURL句柄 CURL* curl = curl_easy_init(); if (curl) { // 设置FTP服务器地址和端口号 string server = "ftp.example.com"; int port = 21; // 设置登录用户名和密码 string username = "username"; string password = "password"; // 设置FTP请求选项 struct curl_slist* headers = NULL; headers = curl_slist_append(headers, "User-Agent: FTP Client"); curl_easy_setopt(curl, CURLOPT_USERAGENT, "FTP Client"); curl_easy_setopt(curl, CURLOPT_USERPWD, (username + ":" + password).c_str()); curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); curl_easy_setopt(curl, CURLOPT_URL, (server + ":" + to_string(port)).c_str()); curl_easy_setopt(curl, CURLOPT_FTPPORT, "-"); // 使用被动模式连接FTP服务器 curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L); // 创建不存在的目录 // 执行FTP请求并获取响应状态码 CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { cerr << "Error: " << curl_easy_strerror(res) << endl; return 1; } // 释放CURL句柄和相关资源 curl_easy_cleanup(curl); curl_slist_free_all(headers); } // 关闭CURL库和程序退出 curl_global_cleanup(); return 0; } ``` 希望以上解答能够帮助您解决问题。如果您还有其他问题,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值