使用libcurl的正确姿势
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的正确姿势的更多相关文章
- 判断是否为gif/png图片的正确姿势
判断是否为gif/png图片的正确姿势 1.在能取到图片后缀的前提下 1 2 3 4 5 6 7 8 9 //假设这是一个网络获取的URL NSString *path = @"http:/ ...
- 在Linux(ubuntu server)上面安装NodeJS的正确姿势
上一篇文章,我介绍了 在Windows中安装NodeJS的正确姿势,这一篇,我们继续来看一下在Linux上面安装和配置NodeJS. 为了保持一致,这里也列举三个方法 第一个方法:通过官网下载安装 h ...
- 程序员取悦女朋友的正确姿势---Tips(iOS美容篇)
前言 女孩子都喜欢用美图工具进行图片美容,近来无事时,特意为某人写了个自定义图片滤镜生成器,安装到手机即可完成自定义滤镜渲染照片.app独一无二,虽简亦繁. JH定律:魔镜:最漂亮的女人是你老婆魔镜: ...
- ios监听ScrollView/TableView滚动的正确姿势
主要介绍 监测tableView垂直滚动的舒畅姿势 监测scrollView/collectionView横向滚动的正确姿势 1.监测tableView垂直滚动的舒畅姿势 通常我们用KVO或者在scr ...
- 玩转 Ceph 的正确姿势
玩转 Ceph 的正确姿势 本文先介绍 Ceph, 然后会聊到一些正确使用 Ceph 的姿势:在集群规模小的时候,Ceph 怎么玩都没问题:但集群大了(到PB级别),这些准则可是保证集群健康运行的不二 ...
- 解锁redis锁的正确姿势
解锁redis锁的正确姿势 redis是php的好朋友,在php写业务过程中,有时候会使用到锁的概念,同时只能有一个人可以操作某个行为.这个时候我们就要用到锁.锁的方式有好几种,php不能在内存中用锁 ...
- jquery选中radio或checkbox的正确姿势
jquery选中radio或checkbox的正确姿势 Intro 前几天突然遇到一个问题,没有任何征兆的..,jquery 选中radio button单选框时,一直没有办法选中,后来查了许多资料, ...
- 程序员节应该写博客之.NET下使用HTTP请求的正确姿势
程序员节应该写博客之.NET下使用HTTP请求的正确姿势 一.前言 去年9月份的时候我看到过外国朋友关于.NET Framework下HttpClient缺陷的分析后对HttpClient有了一定的了 ...
- 使用 win10 的正确姿势
17年9月初,写了第一篇<使用 win10 的正确姿势>,而现在半年多过去,觉得文章得更新一些了,索性直接来个第二版吧. -----2018.3.24 写 一. 重新定义桌面 我的桌面: ...
随机推荐
- Jquery中的事件命名机制
来源:aitangyong的专栏 JQuery中的bind()和unbind(),提供了事件的绑定和取消机制,既可以绑定html默认支持的事件,也能够绑定自定义的事件.JQuery支持自定义事件,这显 ...
- Word图片上传控件卸载教程-Xproer.WordPaster
卸载教程: 卸载控件-IE Windows XP Windows 7(x86) Windows 7(x64) 卸载控件-C ...
- 利用nginx打造个人简历网页
1.下载nginx nginx官方网址:http://nginx.org/ 2.下载和解压 #下载:[root@iZwz9cl4i8oy1reej7o8pmZ soft]# wget http://n ...
- ★ prototype、__proto__ 详解
# var Person = function(name) { this.name = name; } var p = new Person(); //new操作符的操作是 var p = {} p. ...
- 配置React的Babel 6和Webpack 2环境
Facebook的一帮子工程师在忙碌之余开发除了一套前段UI框架React.这个框架最大的有点就在于让UI的开发都基于组件,这样View都是根据props和state变化的. 项目地址:https:/ ...
- leetcode - [7]Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- leetcode - [6]Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- Qt_MainWindow简介
QMainWindow 是Qt框架带来的一个预定义好的主窗口类.按照建立HelloWorld程序建立工程,直接运行,或有一个空窗口. main().cpp #include "mainwin ...
- C语言中:static与extern对变量和函数的作用
1.两者对全局变量 static对全局变量,表示定义一个内部变量 extern对全局变量,表示声明一个外部变量 说明: 1.内部变量:定义的变量只能在本文件中访问,不能被其他文件访问. 2.不同文件中 ...
- python模块补充
一.模块补充 configparser 1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -opti ...