libcurl支持访问http、ftp等各种服务器,下载图片AV什么的不在话下。但其存在多种接口,异步接口也很难以理解,到底什么样的使用姿势才是正确滴?我们来看看可用的体位:

  • easy interface:最简单的同步接口,容易理解,但同步访问实在不是性能之选。至于引入多线程,那是简单问题复杂化。注意异步访问也是以easy interface为基础,所以还是要学习一下:《libcurl教程》
  • multi interface:异步访问接口,性能杠杠滴,但是。。。真的很难理解啊。。。官方文档:《multi interface overview》
    • curl_multi_perform() + select():select()性能不够好,还受到file descriptors不能大于1024的限制。参考《使用libcurl进行异步并发访问与文件上传》

    • curl_multi_socket_action():使用epoll模型,性能最好,但更难懂。。。参考范例

    • curl_multi_perform() + curl_multi_wait():这个据说是facebook做出的伟大贡献(参见《Introducing curl_multi_wait》),保证性能的同时也相对容易使用,强力推荐的姿势。抄录示例代码如下:

        /* curl_multi_test.c
      Clemens Gruber, 2013
      <clemens.gruber@pqgruber.com>
      Code description:
      Requests 4 Web pages via the CURL multi interface
      and checks if the HTTP status code is 200.
      Update: Fixed! The check for !numfds was the problem.
      */ #include <stdio.h>
      #include <stdlib.h>
      #ifndef WIN32
      #include <unistd.h>
      #endif
      #include <curl/multi.h> #define MAX_WAIT_MSECS 30*1000 /* Wait max. 30 seconds */ static const char *urls[] = {
      "http://www.microsoft.com",
      "http://www.yahoo.com",
      "http://www.wikipedia.org",
      "http://slashdot.org"
      };
      #define CNT 4 static size_t cb(char *d, size_t n, size_t l, void *p)
      {
      /* take care of the data here, ignored in this example */
      (void)d;
      (void)p;
      return n*l;
      } static void init(CURLM *cm, int i)
      {
      CURL *eh = curl_easy_init();
      curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
      curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
      curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
      curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
      curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);
      curl_multi_add_handle(cm, eh);
      } int main(void)
      {
      CURLM *cm=NULL;
      CURL *eh=NULL;
      CURLMsg *msg=NULL;
      CURLcode return_code=0;
      int still_running=0, i=0, msgs_left=0;
      int http_status_code;
      const char *szUrl; curl_global_init(CURL_GLOBAL_ALL); cm = curl_multi_init(); for (i = 0; i < CNT; ++i) {
      init(cm, i);
      } curl_multi_perform(cm, &still_running); do {
      int numfds=0;
      int res = curl_multi_wait(cm, NULL, 0, MAX_WAIT_MSECS, &numfds);
      if(res != CURLM_OK) {
      fprintf(stderr, "error: curl_multi_wait() returned %d\n", res);
      return EXIT_FAILURE;
      }
      /*
      if(!numfds) {
      fprintf(stderr, "error: curl_multi_wait() numfds=%d\n", numfds);
      return EXIT_FAILURE;
      }
      */
      curl_multi_perform(cm, &still_running); } while(still_running); while ((msg = curl_multi_info_read(cm, &msgs_left))) {
      if (msg->msg == CURLMSG_DONE) {
      eh = msg->easy_handle; return_code = msg->data.result;
      if(return_code!=CURLE_OK) {
      fprintf(stderr, "CURL error code: %d\n", msg->data.result);
      continue;
      } // Get HTTP status code
      http_status_code=0;
      szUrl = NULL; curl_easy_getinfo(eh, CURLINFO_RESPONSE_CODE, &http_status_code);
      curl_easy_getinfo(eh, CURLINFO_PRIVATE, &szUrl); if(http_status_code==200) {
      printf("200 OK for %s\n", szUrl);
      } else {
      fprintf(stderr, "GET of %s returned http status code %d\n", szUrl, http_status_code);
      } curl_multi_remove_handle(cm, eh);
      curl_easy_cleanup(eh);
      }
      else {
      fprintf(stderr, "error: after curl_multi_info_read(), CURLMsg=%d\n", msg->msg);
      }
      } curl_multi_cleanup(cm); return EXIT_SUCCESS;
      }

使用libcurl的正确姿势的更多相关文章

  1. 判断是否为gif/png图片的正确姿势

    判断是否为gif/png图片的正确姿势 1.在能取到图片后缀的前提下 1 2 3 4 5 6 7 8 9 //假设这是一个网络获取的URL NSString *path = @"http:/ ...

  2. 在Linux(ubuntu server)上面安装NodeJS的正确姿势

    上一篇文章,我介绍了 在Windows中安装NodeJS的正确姿势,这一篇,我们继续来看一下在Linux上面安装和配置NodeJS. 为了保持一致,这里也列举三个方法 第一个方法:通过官网下载安装 h ...

  3. 程序员取悦女朋友的正确姿势---Tips(iOS美容篇)

    前言 女孩子都喜欢用美图工具进行图片美容,近来无事时,特意为某人写了个自定义图片滤镜生成器,安装到手机即可完成自定义滤镜渲染照片.app独一无二,虽简亦繁. JH定律:魔镜:最漂亮的女人是你老婆魔镜: ...

  4. ios监听ScrollView/TableView滚动的正确姿势

    主要介绍 监测tableView垂直滚动的舒畅姿势 监测scrollView/collectionView横向滚动的正确姿势 1.监测tableView垂直滚动的舒畅姿势 通常我们用KVO或者在scr ...

  5. 玩转 Ceph 的正确姿势

    玩转 Ceph 的正确姿势 本文先介绍 Ceph, 然后会聊到一些正确使用 Ceph 的姿势:在集群规模小的时候,Ceph 怎么玩都没问题:但集群大了(到PB级别),这些准则可是保证集群健康运行的不二 ...

  6. 解锁redis锁的正确姿势

    解锁redis锁的正确姿势 redis是php的好朋友,在php写业务过程中,有时候会使用到锁的概念,同时只能有一个人可以操作某个行为.这个时候我们就要用到锁.锁的方式有好几种,php不能在内存中用锁 ...

  7. jquery选中radio或checkbox的正确姿势

    jquery选中radio或checkbox的正确姿势 Intro 前几天突然遇到一个问题,没有任何征兆的..,jquery 选中radio button单选框时,一直没有办法选中,后来查了许多资料, ...

  8. 程序员节应该写博客之.NET下使用HTTP请求的正确姿势

    程序员节应该写博客之.NET下使用HTTP请求的正确姿势 一.前言 去年9月份的时候我看到过外国朋友关于.NET Framework下HttpClient缺陷的分析后对HttpClient有了一定的了 ...

  9. 使用 win10 的正确姿势

    17年9月初,写了第一篇<使用 win10 的正确姿势>,而现在半年多过去,觉得文章得更新一些了,索性直接来个第二版吧. -----2018.3.24 写 一. 重新定义桌面 我的桌面: ...

随机推荐

  1. 深度优先搜索DFS和广度优先搜索BFS

    DFS简介 深度优先搜索,一般会设置一个数组visited记录每个顶点的访问状态,初始状态图中所有顶点均未被访问,从某个未被访问过的顶点开始按照某个原则一直往深处访问,访问的过程中随时更新数组visi ...

  2. i2c_client的生成

    网上很多文档都是介绍源码,包括i2c_client结构体的源码都有贴出,看上去似乎需要手动写该结构体,但实际上,i2c_client的生成是用如下方法. \arch\arm\mach-omap2/bo ...

  3. asp.net 重写URL方法和封装好的DLL

    .net 重写URL方法和封装好的DLL URL重写方法DLL(2.0)

  4. Jersey RESTful WebService框架学习(三)使用@QueryParam

    介绍:@QueryParamuri路径请求参数写在方法的参数中,获得请求路径附带的参数.比如:@QueryParam("desc") String desc 前端控制 <!D ...

  5. poj 1125 Stockbroker Grapevine(最短路径)

    Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a me ...

  6. WordPaster-KesionCMS V9整合教程

    注意:KesionCMS V9使用的是JQuery 1.10.3版本.需要到JQuery UI官网下载JQuery 1.10.3的UI库. JQueryUI官网:http://jqueryui.com ...

  7. 把sublime text打造成python交互终端(windows和Ubuntu)

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7015958.html 把sublime text打造成python交互终端 ...

  8. java理论学时第七节。课后作业。

    对AboutException.java的理解.在try中如果发出某类系统识别的错误,会以throw的形式抛出,在catch中可以将其截获,不显示在前端,可以选择执行别的代码. ArrayIndexO ...

  9. Jack Straws(poj 1127) 两直线是否相交模板

    http://poj.org/problem?id=1127   Description In the game of Jack Straws, a number of plastic or wood ...

  10. Leetcode--1. Two Sum(easy)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...