Git init
[external/curl.git] / docs / examples / multi-app.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  *
9  * This is an example application source code using the multi interface.
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 /* somewhat unix-specific */
16 #include <sys/time.h>
17 #include <unistd.h>
18
19 /* curl stuff */
20 #include <curl/curl.h>
21
22 /*
23  * Download a HTTP file and upload an FTP file simultaneously.
24  */
25
26 #define HANDLECOUNT 2   /* Number of simultaneous transfers */
27 #define HTTP_HANDLE 0   /* Index for the HTTP transfer */
28 #define FTP_HANDLE 1    /* Index for the FTP transfer */
29
30 int main(int argc, char **argv)
31 {
32   CURL *handles[HANDLECOUNT];
33   CURLM *multi_handle;
34
35   int still_running; /* keep number of running handles */
36   int i;
37
38   CURLMsg *msg; /* for picking up messages with the transfer status */
39   int msgs_left; /* how many messages are left */
40
41   /* Allocate one CURL handle per transfer */
42   for (i=0; i<HANDLECOUNT; i++)
43       handles[i] = curl_easy_init();
44
45   /* set the options (I left out a few, you'll get the point anyway) */
46   curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
47
48   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
49   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
50
51   /* init a multi stack */
52   multi_handle = curl_multi_init();
53
54   /* add the individual transfers */
55   for (i=0; i<HANDLECOUNT; i++)
56       curl_multi_add_handle(multi_handle, handles[i]);
57
58   /* we start some action by calling perform right away */
59   curl_multi_perform(multi_handle, &still_running);
60
61   while(still_running) {
62     struct timeval timeout;
63     int rc; /* select() return code */
64
65     fd_set fdread;
66     fd_set fdwrite;
67     fd_set fdexcep;
68     int maxfd = -1;
69
70     long curl_timeo = -1;
71
72     FD_ZERO(&fdread);
73     FD_ZERO(&fdwrite);
74     FD_ZERO(&fdexcep);
75
76     /* set a suitable timeout to play around with */
77     timeout.tv_sec = 1;
78     timeout.tv_usec = 0;
79
80     curl_multi_timeout(multi_handle, &curl_timeo);
81     if(curl_timeo >= 0) {
82       timeout.tv_sec = curl_timeo / 1000;
83       if(timeout.tv_sec > 1)
84         timeout.tv_sec = 1;
85       else
86         timeout.tv_usec = (curl_timeo % 1000) * 1000;
87     }
88
89     /* get file descriptors from the transfers */
90     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
91
92     /* In a real-world program you OF COURSE check the return code of the
93        function calls.  On success, the value of maxfd is guaranteed to be
94        greater or equal than -1.  We call select(maxfd + 1, ...), specially in
95        case of (maxfd == -1), we call select(0, ...), which is basically equal
96        to sleep. */
97
98     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
99
100     switch(rc) {
101     case -1:
102       /* select error */
103       break;
104     case 0: /* timeout */
105     default: /* action */
106       curl_multi_perform(multi_handle, &still_running);
107       break;
108     }
109   }
110
111   /* See how the transfers went */
112   while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
113     if (msg->msg == CURLMSG_DONE) {
114       int idx, found = 0;
115
116       /* Find out which handle this message is about */
117       for (idx=0; idx<HANDLECOUNT; idx++) {
118         found = (msg->easy_handle == handles[idx]);
119         if(found)
120           break;
121       }
122
123       switch (idx) {
124       case HTTP_HANDLE:
125         printf("HTTP transfer completed with status %d\n", msg->data.result);
126         break;
127       case FTP_HANDLE:
128         printf("FTP transfer completed with status %d\n", msg->data.result);
129         break;
130       }
131     }
132   }
133
134   curl_multi_cleanup(multi_handle);
135
136   /* Free the CURL handles */
137   for (i=0; i<HANDLECOUNT; i++)
138       curl_easy_cleanup(handles[i]);
139
140   return 0;
141 }