Return to multi-double.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / curl / docs / examples |
1.1 ! misho 1: /*************************************************************************** ! 2: * _ _ ____ _ ! 3: * Project ___| | | | _ \| | ! 4: * / __| | | | |_) | | ! 5: * | (__| |_| | _ <| |___ ! 6: * \___|\___/|_| \_\_____| ! 7: * ! 8: * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al. ! 9: * ! 10: * This software is licensed as described in the file COPYING, which ! 11: * you should have received as part of this distribution. The terms ! 12: * are also available at https://curl.haxx.se/docs/copyright.html. ! 13: * ! 14: * You may opt to use, copy, modify, merge, publish, distribute and/or sell ! 15: * copies of the Software, and permit persons to whom the Software is ! 16: * furnished to do so, under the terms of the COPYING file. ! 17: * ! 18: * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY ! 19: * KIND, either express or implied. ! 20: * ! 21: ***************************************************************************/ ! 22: /* <DESC> ! 23: * multi interface code doing two parallel HTTP transfers ! 24: * </DESC> ! 25: */ ! 26: #include <stdio.h> ! 27: #include <string.h> ! 28: ! 29: /* somewhat unix-specific */ ! 30: #include <sys/time.h> ! 31: #include <unistd.h> ! 32: ! 33: /* curl stuff */ ! 34: #include <curl/curl.h> ! 35: ! 36: /* ! 37: * Simply download two HTTP files! ! 38: */ ! 39: int main(void) ! 40: { ! 41: CURL *http_handle; ! 42: CURL *http_handle2; ! 43: CURLM *multi_handle; ! 44: ! 45: int still_running = 0; /* keep number of running handles */ ! 46: ! 47: http_handle = curl_easy_init(); ! 48: http_handle2 = curl_easy_init(); ! 49: ! 50: /* set options */ ! 51: curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/"); ! 52: ! 53: /* set options */ ! 54: curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/"); ! 55: ! 56: /* init a multi stack */ ! 57: multi_handle = curl_multi_init(); ! 58: ! 59: /* add the individual transfers */ ! 60: curl_multi_add_handle(multi_handle, http_handle); ! 61: curl_multi_add_handle(multi_handle, http_handle2); ! 62: ! 63: /* we start some action by calling perform right away */ ! 64: curl_multi_perform(multi_handle, &still_running); ! 65: ! 66: while(still_running) { ! 67: struct timeval timeout; ! 68: int rc; /* select() return code */ ! 69: CURLMcode mc; /* curl_multi_fdset() return code */ ! 70: ! 71: fd_set fdread; ! 72: fd_set fdwrite; ! 73: fd_set fdexcep; ! 74: int maxfd = -1; ! 75: ! 76: long curl_timeo = -1; ! 77: ! 78: FD_ZERO(&fdread); ! 79: FD_ZERO(&fdwrite); ! 80: FD_ZERO(&fdexcep); ! 81: ! 82: /* set a suitable timeout to play around with */ ! 83: timeout.tv_sec = 1; ! 84: timeout.tv_usec = 0; ! 85: ! 86: curl_multi_timeout(multi_handle, &curl_timeo); ! 87: if(curl_timeo >= 0) { ! 88: timeout.tv_sec = curl_timeo / 1000; ! 89: if(timeout.tv_sec > 1) ! 90: timeout.tv_sec = 1; ! 91: else ! 92: timeout.tv_usec = (curl_timeo % 1000) * 1000; ! 93: } ! 94: ! 95: /* get file descriptors from the transfers */ ! 96: mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); ! 97: ! 98: if(mc != CURLM_OK) { ! 99: fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); ! 100: break; ! 101: } ! 102: ! 103: /* On success the value of maxfd is guaranteed to be >= -1. We call ! 104: select(maxfd + 1, ...); specially in case of (maxfd == -1) there are ! 105: no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- ! 106: to sleep 100ms, which is the minimum suggested value in the ! 107: curl_multi_fdset() doc. */ ! 108: ! 109: if(maxfd == -1) { ! 110: #ifdef _WIN32 ! 111: Sleep(100); ! 112: rc = 0; ! 113: #else ! 114: /* Portable sleep for platforms other than Windows. */ ! 115: struct timeval wait = { 0, 100 * 1000 }; /* 100ms */ ! 116: rc = select(0, NULL, NULL, NULL, &wait); ! 117: #endif ! 118: } ! 119: else { ! 120: /* Note that on some platforms 'timeout' may be modified by select(). ! 121: If you need access to the original value save a copy beforehand. */ ! 122: rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout); ! 123: } ! 124: ! 125: switch(rc) { ! 126: case -1: ! 127: /* select error */ ! 128: break; ! 129: case 0: ! 130: default: ! 131: /* timeout or readable/writable sockets */ ! 132: curl_multi_perform(multi_handle, &still_running); ! 133: break; ! 134: } ! 135: } ! 136: ! 137: curl_multi_cleanup(multi_handle); ! 138: ! 139: curl_easy_cleanup(http_handle); ! 140: curl_easy_cleanup(http_handle2); ! 141: ! 142: return 0; ! 143: }