Advertisement
Guest User

multi-poll.c

a guest
Mar 12th, 2021
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. /* somewhat unix-specific */
  5. #include <sys/time.h>
  6. #include <unistd.h>
  7.  
  8. /* curl stuff */
  9. #include <curl/curl.h>
  10.  
  11. #define COUNT 10
  12. #define URL "example.org:8200"
  13.  
  14. int main(void)
  15. {
  16.   CURL *http_handles[COUNT];
  17.   CURLM *multi_handle;
  18.   int still_running = 1; /* keep number of running handles */
  19.  
  20.   curl_global_init(CURL_GLOBAL_DEFAULT);
  21.  
  22.   multi_handle = curl_multi_init();
  23.   curl_multi_setopt(multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, 1l);
  24.  
  25.   for (int i = 0 ; i < COUNT ; i++) {
  26.     http_handles[i] = curl_easy_init();
  27.     curl_easy_setopt(http_handles[i], CURLOPT_URL, URL);
  28.     curl_easy_setopt(http_handles[i], CURLOPT_TIMEOUT_MS, 1000l);
  29.     curl_multi_add_handle(multi_handle, http_handles[i]);
  30.   }
  31.  
  32.   while(still_running) {
  33.     CURLMcode mc; /* curl_multi_poll() return code */
  34.     int numfds;
  35.  
  36.     /* we start some action by calling perform right away */
  37.     mc = curl_multi_perform(multi_handle, &still_running);
  38.  
  39.     if(still_running)
  40.       /* wait for activity, timeout or "nothing" */
  41.       mc = curl_multi_poll(multi_handle, NULL, 0, 100, &numfds);
  42.  
  43.     if(mc != CURLM_OK) {
  44.       fprintf(stderr, "curl_multi_wait() failed, code %d.\n", mc);
  45.       break;
  46.     }
  47.   }
  48.  
  49.   for (int i = 0 ; i < COUNT ; i++) {
  50.     curl_multi_remove_handle(multi_handle, http_handles[i]);
  51.     curl_easy_cleanup(http_handles[i]);
  52.   }
  53.  
  54.   curl_multi_cleanup(multi_handle);
  55.   curl_global_cleanup();
  56.  
  57.   return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement