Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / multi-app.c
blob16c9d720901459de92acc9439c8ef6b6375cad9d
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: multi-app.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
10 * This is an example application source code using the multi interface.
13 #include <stdio.h>
14 #include <string.h>
16 /* somewhat unix-specific */
17 #include <sys/time.h>
18 #include <unistd.h>
20 /* curl stuff */
21 #include <curl/curl.h>
24 * Download a HTTP file and upload an FTP file simultaneously.
27 #define HANDLECOUNT 2 /* Number of simultaneous transfers */
28 #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
29 #define FTP_HANDLE 1 /* Index for the FTP transfer */
31 int main(int argc, char **argv)
33 CURL *handles[HANDLECOUNT];
34 CURLM *multi_handle;
36 int still_running; /* keep number of running handles */
37 int i;
39 CURLMsg *msg; /* for picking up messages with the transfer status */
40 int msgs_left; /* how many messages are left */
42 /* Allocate one CURL handle per transfer */
43 for (i=0; i<HANDLECOUNT; i++)
44 handles[i] = curl_easy_init();
46 /* set the options (I left out a few, you'll get the point anyway) */
47 curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://website.com");
49 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://ftpsite.com");
50 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
52 /* init a multi stack */
53 multi_handle = curl_multi_init();
55 /* add the individual transfers */
56 for (i=0; i<HANDLECOUNT; i++)
57 curl_multi_add_handle(multi_handle, handles[i]);
59 /* we start some action by calling perform right away */
60 while(CURLM_CALL_MULTI_PERFORM ==
61 curl_multi_perform(multi_handle, &still_running));
63 while(still_running) {
64 struct timeval timeout;
65 int rc; /* select() return code */
67 fd_set fdread;
68 fd_set fdwrite;
69 fd_set fdexcep;
70 int maxfd;
72 FD_ZERO(&fdread);
73 FD_ZERO(&fdwrite);
74 FD_ZERO(&fdexcep);
76 /* set a suitable timeout to play around with */
77 timeout.tv_sec = 1;
78 timeout.tv_usec = 0;
80 /* get file descriptors from the transfers */
81 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
83 /* In a real-world program you OF COURSE check the return code of the
84 function calls, *and* you make sure that maxfd is bigger than -1 so
85 that the call to select() below makes sense! */
87 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
89 switch(rc) {
90 case -1:
91 /* select error */
92 break;
93 case 0:
94 /* timeout, do something else */
95 break;
96 default:
97 /* one or more of curl's file descriptors say there's data to read
98 or write */
99 while(CURLM_CALL_MULTI_PERFORM ==
100 curl_multi_perform(multi_handle, &still_running));
101 break;
105 /* See how the transfers went */
106 while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
107 if (msg->msg == CURLMSG_DONE) {
109 int idx, found = 0;
111 /* Find out which handle this message is about */
112 for (idx=0; (!found && (idx<HANDLECOUNT)); idx++) found = (msg->easy_handle == handles[idx]);
114 switch (idx) {
115 case HTTP_HANDLE:
116 printf("HTTP transfer completed with status %d\n", msg->data.result);
117 break;
118 case FTP_HANDLE:
119 printf("FTP transfer completed with status %d\n", msg->data.result);
120 break;
125 curl_multi_cleanup(multi_handle);
127 /* Free the CURL handles */
128 for (i=0; i<HANDLECOUNT; i++)
129 curl_easy_cleanup(handles[i]);
131 return 0;