Commit a95df824 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

Henrik Storner's rewrite that includes a nice usage of curl_multi_info_read()

parent 8feb5110
Loading
Loading
Loading
Loading
+40 −11
Original line number Diff line number Diff line
@@ -23,29 +23,38 @@
/*
 * Download a HTTP file and upload an FTP file simultaneously.
 */

#define HANDLECOUNT 2	/* Number of simultaneous transfers */
#define HTTP_HANDLE 0	/* Index for the HTTP transfer */
#define FTP_HANDLE 1	/* Index for the FTP transfer */

int main(int argc, char **argv)
{
  CURL *http_handle;
  CURL *ftp_handle;
  CURL *handles[HANDLECOUNT];
  CURLM *multi_handle;

  int still_running; /* keep number of running handles */
  int i;

  CURLMsg *msg; /* for picking up messages with the transfer status */
  int msgs_left; /* how many messages are left */

  http_handle = curl_easy_init();
  ftp_handle  = curl_easy_init();
  /* Allocate one CURL handle per transfer */
  for (i=0; i<HANDLECOUNT; i++)
      handles[i] = curl_easy_init();

  /* set the options (I left out a few, you'll get the point anyway) */
  curl_easy_setopt(http_handle, CURLOPT_URL, "http://website.com");
  curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://website.com");

  curl_easy_setopt(ftp_handle, CURLOPT_URL, "ftp://ftpsite.com");
  curl_easy_setopt(ftp_handle, CURLOPT_UPLOAD, TRUE);
  curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://ftpsite.com");
  curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, TRUE);

  /* init a multi stack */
  multi_handle = curl_multi_init();

  /* add the individual transfers */
  curl_multi_add_handle(multi_handle, http_handle);
  curl_multi_add_handle(multi_handle, ftp_handle);
  for (i=0; i<HANDLECOUNT; i++)
      curl_multi_add_handle(multi_handle, handles[i]);

  /* we start some action by calling perform right away */
  while(CURLM_CALL_MULTI_PERFORM ==
@@ -91,9 +100,29 @@ int main(int argc, char **argv)

  curl_multi_cleanup(multi_handle);

  curl_easy_cleanup(http_handle);
  curl_easy_cleanup(ftp_handle);
  /* See how the transfers went */
  while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
    if (msg->msg == CURLMSG_DONE) {

       int idx, found = 0;

       /* Find out which handle this message is about */
       for (idx=0; (!found && (idx<HANDLECOUNT)); idx++) found = (msg->easy_handle == handles[idx]);

       switch (idx) {
         case HTTP_HANDLE:
	   printf("HTTP transfer completed with status %d\n", msg->data.result);
	   break;
	 case FTP_HANDLE:
	   printf("FTP transfer completed with status %d\n", msg->data.result);
	   break;
       }
    }
  }

  /* Free the CURL handles */
  for (i=0; i<HANDLECOUNT; i++)
      curl_easy_cleanup(handles[i]);

  return 0;
}