libcurl同时下载多个文件
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <curl/multi.h> static const char *urls[] = {
"http://www.microsoft.com",
"http://www.opensource.org",
"http://www.google.com",
"http://www.yahoo.com",
"http://www.ibm.com",
"http://www.mysql.com",
"http://www.oracle.com",
"http://www.ripe.net",
"http://www.iana.org",
"http://www.amazon.com",
"http://www.netcraft.com",
"http://www.heise.de",
"http://www.chip.de",
"http://www.ca.com",
"http://www.cnet.com",
"http://www.news.com",
"http://www.cnn.com",
"http://www.wikipedia.org",
"http://www.dell.com",
"http://www.hp.com",
"http://www.cert.org",
"http://www.mit.edu",
"http://www.nist.gov"
}; #define MAX 10
#define CNT sizeof(urls)/sizeof(char*) 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;
}
int nIndex=0;
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_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);查询出来
curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L); //如果你想CURL报告每一件意外的事情,设置这个选项为一个非零值
curl_easy_setopt(eh, CURLOPT_WRITEDATA, (void *)&nIndex); //设置调用cb函数时传递cb函数的第四个参数
curl_easy_setopt(eh, CURLOPT_USERAGENT, "Lavf/55.13.102"); //设置useragent
curl_multi_add_handle(cm, eh);
} int main(void)
{
CURLM *cm;
CURLMsg *msg;
long L;
unsigned int C=0;
int M, Q, U = -1;
fd_set R, W, E;
struct timeval T; curl_global_init(CURL_GLOBAL_ALL); cm = curl_multi_init(); /* we can optionally limit the total amount of connections this multi handle
uses */
curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX); for(C = 0; C < MAX; ++C) {
init(cm, C);
} while(U) {
curl_multi_perform(cm, &U); //执行并发请求,非阻塞,立即返回 if(U) {
FD_ZERO(&R);
FD_ZERO(&W);
FD_ZERO(&E);
//获取cm需要监听的文件描述符集合,如果返回的M等于-1,需要等一会然后再次调用curl_multi_perform,等多久?建议至少100毫秒,但你可能想在你自己的特定条件下测试它,找到一个合适的值
if(curl_multi_fdset(cm, &R, &W, &E, &M)) {
fprintf(stderr, "E: curl_multi_fdset\n");
return EXIT_FAILURE;
} if(curl_multi_timeout(cm, &L)) {
fprintf(stderr, "E: curl_multi_timeout\n");
return EXIT_FAILURE;
}
if(L == -1)
L = 100; if(M == -1) {
#ifdef WIN32
Sleep(L);
#else
sleep((unsigned int)L / 1000);
#endif
}
else {
T.tv_sec = L/1000;
T.tv_usec = (L%1000)*1000;
/*确定一个或多个套接口的状态,可查询它的可读性、可写性及错误状态信息。
返回值:
select()调用返回处于就绪状态并且已经包含在fd_set结构中的描述字总数;如果超时则返回0;否则的话,返回SOCKET_ERROR(-1)错误,应用程序可通过WSAGetLastError()获取相应错误代码。
*/
if(0 > select(M+1, &R, &W, &E, &T)) {
fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n",M+1, L, errno, strerror(errno));
return EXIT_FAILURE;
}
}
}
/*获取当前解析的cURL的相关传输信息
查询批处理句柄是否单独的传输线程中有消息或信息返回。消息可能包含诸如从单独的传输线程返回的错误码或者只是传输线程有没有完成之类的报告。
重复调用这个函数,它每次都会返回一个新的结果,直到这时没有更多信息返回时,FALSE 被当作一个信号返回。通过msgs_in_queue返回的整数指出将会包含当这次函数被调用后,还剩余的消息数。
Warning
返回的资源指向的数据调用curl_multi_remove_handle()后将不会存在。 */ while((msg = curl_multi_info_read(cm, &Q))) {
if(msg->msg == CURLMSG_DONE) {
char *url;
CURL *e = msg->easy_handle;
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
fprintf(stderr, "R: %d - %s <%s>\n",
msg->data.result, curl_easy_strerror(msg->data.result), url);
curl_multi_remove_handle(cm, e);
curl_easy_cleanup(e);
}
else {
fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
}
if(C < CNT) {
init(cm, C++);
U++; /* just to prevent it from remaining at 0 if there are more
URLs to get */
}
}
} curl_multi_cleanup(cm);
curl_global_cleanup(); return EXIT_SUCCESS;
}
libcurl同时下载多个文件的更多相关文章
- 如何下载Github单个文件(Windows平台)
如何下载Github单个文件(Windows平台) 前提 安装Chrome 浏览器 Chrome浏览器 安装迅雷软件 安装Chrome 迅雷插件 可能商店里迅雷插件有好几种,这里使用这一种 一般使用者 ...
- IOS下载查看PDF文件(有下载进度)
IOS(object-c) 下载查看 PDF 其实还是蛮容易操作的.在下载前,首先要把 IOS 可以保存文件的目录给过一遍: IOS 文件保存目录 IOS 可以自定义写入的文件目录,是很有限的,只能是 ...
- 用DOS批处理实现FTP自动上传、下载、清理文件
用DOS批处理实现FTP自动上传.下载.清理文件 最近好像特别的忙,好久没来写点东西了,今天写了一个利用批处理程序完成FTP自动上传.下载.清理文件的程序.赶紧 记录下来,以备日后之用.功能介绍:自动 ...
- Ubuntu安装已经下载好的文件包
默认的文件下载都在 ~/Downloads 文件夹里面. 按 ctrl+alt+t 打开命令. 1.解压下载好的文件包,如: tar -xvf Sublime\ Text\ 2.0.2.tar.bz2 ...
- 使用wget -i下载多个文件
使用wget -i下载多个文件 命令: wget -i filelist.txt 说明: 首先,保存一份下载链接文件 cat > filelist.txt url1 url2 url3 url4
- https 协议下服务器根据网络地址下载上传文件问题
https 协议下服务器根据网络地址下载上传文件遇到(PKIX:unable to find valid certification path to requested target 的问题) 使用h ...
- Asp.Net MVC 实现将Easy-UI展示数据下载为Excel 文件
在一个项目中,需要做一个将Easy-UI界面展示数据下载为Excel文件的功能,经过一段时间努力,完成了一个小Demo.界面如下: 但按下导出Excel后,Excel文件将会下载到本地,在office ...
- 在windows下使用cmd命令全速下载百度云文件
在windows下使用cmd命令全速下载百度云文件 需要的工具BaiduPCS-GO(链接:https://pan.baidu.com/s/19Sn8gmNi_GZHJwUPu79DPg 密码:gqi ...
- git下载/上传文件提示:git did not exit cleanly
问题:git操作下载/上传文件,提示信息如下 TortoiseGit-git did not exit cleanly (exit code 1) TortoiseGit-git did not ex ...
随机推荐
- 快排,归并和Shell排序
快速排序 快速排序的执行流程: (1) 先从数列中取出一个数作为基准数. (2) 将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边. (3)再对左右区间重复第二步,直到各区间只有一个数. ...
- Python---战机小游戏,学习pygame
import pygame # 导入游戏包 pygame.init() # 导入并初始化所有pygame模块,使用其他模块之前必须先调用init()方法 print('下面是游戏代码:') # 绘制矩 ...
- MVC下拉框Html.DropDownList 和DropDownListFor 的常用方法
一.非强类型:Controller:ViewData["AreId"] = from a in Table select ...
- 区别js中name与id的简单方法
举个简单的例子: <form name="form1"> 用户名:<input type=text name="username" id=&q ...
- [转]Extending the User Interface in Outlook 2010
本文转自:https://msdn.microsoft.com/en-us/library/office/ee692172%28v=office.14%29.aspx#OfficeOLExtendin ...
- 在懒加载的Ionic工程中使用 ionic2-auto-complete 组件:Can't bind to 'dataProvider' since it isn't a known property of 'ion-auto-complete'
问题描述: 在基于懒加载的Ionic工程中,使用ionic2-auto-complete组件(GitHub地址:https://github.com/kadoshms/ionic2-autocompl ...
- 【转】类找不到总结java.lang.ClassNotFoundException
(1)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqls ...
- Spring学习手札(三)理解IoC 拯救不开心
Inverse of Control,翻译成“控制反转”,是Spring的核心.IoC不是一种技术,而是一种设计思想.就是将原本在程序中手动创建对象的控制权(new Object() ),交由Spri ...
- bzoj2111ZJ2010排列计数_solution
-by bzoj http://www.lydsy.com/JudgeOnline/problem.php?id=2111 考虑第i个位置上的数字的可能性只取决于第i/2位置上的数,以及剩余数集的大小 ...
- Math.random理解练习
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...