multi接口的使用会比easy 接口稍微复杂点,毕竟multi接口是依赖easy接口的,首先粗略的讲下其使用流程:curl_multi _init初始化一个multi curl对象,为了同时进行多个curl的并发访问,我们需要初始化多个easy curl对象,使用curl_easy_setopt进行相关设置,然后调用curl_multi _add_handle把easy curl对象添加到multi curl对象中,添加完毕后执行curl_multi_perform方法进行并发的访问,访问结束后curl_multi_remove_handle移除相关easy curl对象,curl_easy_cleanup清除easy curl对象,最后curl_multi_cleanup清除multi curl对象。
// CurlDownload.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
# include <unistd.h>
#endif
#include <curl/multi.h>
#pragma comment(lib,"libcurl.lib")
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",
"http://www.ebay.com",
"http://www.playstation.com",
"http://www.uefa.com",
"http://www.ieee.org",
"http://www.apple.com",
"http://www.sony.com",
"http://www.symantec.com",
"http://www.zdnet.com",
"http://www.fujitsu.com",
"http://www.supermicro.com",
"http://www.hotmail.com",
"http://www.ecma.com",
"http://www.bbc.co.uk",
"http://news.google.com",
"http://www.foxnews.com",
"http://www.msn.com",
"http://www.wired.com",
"http://www.sky.com",
"http://www.usatoday.com",
"http://www.cbs.com",
"http://www.nbc.com",
"http://slashdot.org",
"http://www.bloglines.com",
"http://www.techweb.com",
"http://www.newslink.org",
"http://www.un.org",
};
#define MAX 10 //最大连接数(同时)
#define CNT sizeof(urls)/sizeof(char*) //连接的总数
static size_t cb(char *d, size_t n, size_t l, void *p)
{
//注意函数的返回值
(void)d;
(void)p;
return n*l;
}
static void init(CURLM *cm, int i)
{
// 设置easy curl对象并添加到multi curl对象中
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 _tmain(int argc, _TCHAR* argv[])
{
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();
//这里可以在multi handle里面设置最大连接总数
curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX);
for (C = 0; C < MAX; ++C) {
init(cm, C);
}
//为了避免循环调用curl_multi_perform产生的cpu持续占用的问题,采用select来监听文件描述符
while (U) {
curl_multi_perform(cm, &U);
if (U) { // 注意这里一定要清空fdset,curl_multi_fdset不会执行fdset的清空操作 //
FD_ZERO(&R);
FD_ZERO(&W);
FD_ZERO(&E);
if (curl_multi_fdset(cm, &R, &W, &E, &M)) { // 获取multi curl需要监听的文件描述符集合 fd_set //
fprintf(stderr, "E: curl_multi_fdset\n");
return EXIT_FAILURE;
}
if (curl_multi_timeout(cm, &L)) { //获取multi curl的超时
fprintf(stderr, "E: curl_multi_timeout\n");
return EXIT_FAILURE;
}
if (L == -1)
L = 100;
if (M == -1) {
#ifdef WIN32
Sleep(L);
#else
sleep(L / 1000);
#endif
} else {
T.tv_sec = L/1000; // 设置select超时时间 //
T.tv_usec = (L%1000)*1000;
//使用select函数来监听multi curl文件描述符的状态,监听成功返回0,监听失败返回-1
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;
}
}
}
while ((msg = curl_multi_info_read(cm, &Q))) { //读取multi curl的消息,判断是否连接成功
if (msg->msg == CURLMSG_DONE) {
char *url;
CURL *e = msg->easy_handle;
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url); //连接成功的url
fprintf(stderr, "R: %d - %s <%s>\n",
msg->data.result, curl_easy_strerror(msg->data.result), url);
curl_multi_remove_handle(cm, e); //这里需要注意清除handle的顺序
curl_easy_cleanup(e);
}
else {
fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
}
if (C < CNT) {
init(cm, C++);
U++;
}
}
}
curl_multi_cleanup(cm);
curl_global_cleanup();
return EXIT_SUCCESS;
}
程序运行: