*** empty log message ***
[platform/upstream/curl.git] / docs / examples / multi-app.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  *
10  * This is an example application source code using the multi interface.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 /* somewhat unix-specific */
17 #include <sys/time.h>
18 #include <unistd.h>
19
20 /* curl stuff */
21 #include <curl/curl.h>
22
23 /*
24  * Download a HTTP file and upload an FTP file simultaneously.
25  */
26
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 */
30
31 int main(int argc, char **argv)
32 {
33   CURL *handles[HANDLECOUNT];
34   CURLM *multi_handle;
35
36   int still_running; /* keep number of running handles */
37   int i;
38
39   CURLMsg *msg; /* for picking up messages with the transfer status */
40   int msgs_left; /* how many messages are left */
41
42   /* Allocate one CURL handle per transfer */
43   for (i=0; i<HANDLECOUNT; i++)
44       handles[i] = curl_easy_init();
45
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");
48
49   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://ftpsite.com");
50   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, TRUE);
51
52   /* init a multi stack */
53   multi_handle = curl_multi_init();
54
55   /* add the individual transfers */
56   for (i=0; i<HANDLECOUNT; i++)
57       curl_multi_add_handle(multi_handle, handles[i]);
58
59   /* we start some action by calling perform right away */
60   while(CURLM_CALL_MULTI_PERFORM ==
61         curl_multi_perform(multi_handle, &still_running));
62
63   while(still_running) {
64     struct timeval timeout;
65     int rc; /* select() return code */
66
67     fd_set fdread;
68     fd_set fdwrite;
69     fd_set fdexcep;
70     int maxfd;
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     /* get file descriptors from the transfers */
81     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
82
83     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
84
85     switch(rc) {
86     case -1:
87       /* select error */
88       break;
89     case 0:
90       /* timeout, do something else */
91       break;
92     default:
93       /* one or more of curl's file descriptors say there's data to read
94          or write */
95       while(CURLM_CALL_MULTI_PERFORM ==
96             curl_multi_perform(multi_handle, &still_running));
97       break;
98     }
99   }
100
101   /* See how the transfers went */
102   while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
103     if (msg->msg == CURLMSG_DONE) {
104
105        int idx, found = 0;
106
107        /* Find out which handle this message is about */
108        for (idx=0; (!found && (idx<HANDLECOUNT)); idx++) found = (msg->easy_handle == handles[idx]);
109
110        switch (idx) {
111          case HTTP_HANDLE:
112            printf("HTTP transfer completed with status %d\n", msg->data.result);
113            break;
114          case FTP_HANDLE:
115            printf("FTP transfer completed with status %d\n", msg->data.result);
116            break;
117        }
118     }
119   }
120
121   curl_multi_cleanup(multi_handle);
122
123   /* Free the CURL handles */
124   for (i=0; i<HANDLECOUNT; i++)
125       curl_easy_cleanup(handles[i]);
126
127   return 0;
128 }