From 9fc62a8dd0e940668c8ee6310fa9d4530cda744a Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 4 Mar 2002 10:15:44 +0000 Subject: multi interface using examples --- docs/examples/multi-double.c | 87 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 docs/examples/multi-double.c (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c new file mode 100644 index 000000000..c850b0fe4 --- /dev/null +++ b/docs/examples/multi-double.c @@ -0,0 +1,87 @@ +/* + * This is a simple example using the multi interface. + */ + +#include +#include + +/* somewhat unix-specific */ +#include +#include + +/* To start with, we include the header from the lib directory. This should + later of course be moved to the proper include dir. */ +#include "../lib/multi.h" + +/* + * Simply download two HTTP files! + */ +int main(int argc, char **argv) +{ + CURL *http_handle; + CURL *http_handle2; + CURLM *multi_handle; + + int still_running; /* keep number of running handles */ + + http_handle = curl_easy_init(); + http_handle2 = curl_easy_init(); + + /* set options */ + curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/"); + + /* set options */ + curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/"); + + /* 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, http_handle2); + + /* we start some action by calling perform right away */ + while(CURLM_CALL_MULTI_PERFORM == + curl_multi_perform(multi_handle, &still_running)); + + while(still_running) { + struct timeval timeout; + int rc; /* select() return code */ + + fd_set fdread; + fd_set fdwrite; + fd_set fdexcep; + int maxfd; + + FD_ZERO(&fdread); + FD_ZERO(&fdwrite); + FD_ZERO(&fdexcep); + + /* set a suitable timeout to play around with */ + timeout.tv_sec = 1; + timeout.tv_usec = 0; + + /* get file descriptors from the transfers */ + curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); + + rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); + + switch(rc) { + case -1: + /* select error */ + break; + case 0: + default: + /* timeout or readable/writable sockets */ + curl_multi_perform(multi_handle, &still_running); + break; + } + } + + curl_multi_cleanup(multi_handle); + + curl_easy_cleanup(http_handle); + curl_easy_cleanup(http_handle2); + + return 0; +} -- cgit v1.2.1 From 28939dd45cf0f21c039af961120ee25f2bd391f9 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 19 Mar 2002 14:00:47 +0000 Subject: fixed include and added header --- docs/examples/multi-double.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index c850b0fe4..2b92d55f5 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -1,5 +1,13 @@ -/* - * This is a simple example using the multi interface. +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * $Id$ + * + * This is a very simple example using the multi interface. */ #include @@ -9,9 +17,8 @@ #include #include -/* To start with, we include the header from the lib directory. This should - later of course be moved to the proper include dir. */ -#include "../lib/multi.h" +/* curl stuff */ +#include /* * Simply download two HTTP files! -- cgit v1.2.1 From a39cdc80b7ed6d98612a718e7eec63bb44a4a46b Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 3 Dec 2002 12:34:43 +0000 Subject: Jeff pointed out this flaw in the example --- docs/examples/multi-double.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 2b92d55f5..51b4ed370 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -80,7 +80,8 @@ int main(int argc, char **argv) case 0: default: /* timeout or readable/writable sockets */ - curl_multi_perform(multi_handle, &still_running); + while(CURLM_CALL_MULTI_PERFORM == + curl_multi_perform(multi_handle, &still_running)); break; } } -- cgit v1.2.1 From f53347631eb4a5a075589e6fece43aced010a5bb Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 13 Oct 2006 14:01:19 +0000 Subject: Added comments about checking return code and the maxfd counter --- docs/examples/multi-double.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 51b4ed370..0a4cde855 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -71,6 +71,10 @@ int main(int argc, char **argv) /* get file descriptors from the transfers */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); + /* In a real-world program you OF COURSE check the return code of the + function calls, *and* you make sure that maxfd is bigger than -1 so + that the call to select() below makes sense! */ + rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) { -- cgit v1.2.1 From a07bc79117971b96ebf3188c0a34a73ee0a3609b Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 14 Feb 2010 19:40:18 +0000 Subject: removed trailing whitespace --- docs/examples/multi-double.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 0a4cde855..2fb6973bb 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -1,8 +1,8 @@ /***************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * $Id$ -- cgit v1.2.1 From 2309b4e330b96bc2e1f8e36b6184015e59544037 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 24 Mar 2010 11:02:54 +0100 Subject: remove the CVSish $Id$ lines --- docs/examples/multi-double.c | 1 - 1 file changed, 1 deletion(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 2fb6973bb..ef3bf92fc 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -5,7 +5,6 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * $Id$ * * This is a very simple example using the multi interface. */ -- cgit v1.2.1 From d487ade72c5f31703ce097e8460e0225fad80348 Mon Sep 17 00:00:00 2001 From: Kamil Dudka Date: Sat, 24 Apr 2010 12:14:21 +0200 Subject: test536: do not fail with threaded DNS resolver Also tweaked comments in certain examples using curl_multi_fdset(). --- docs/examples/multi-double.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index ef3bf92fc..bc5b446ea 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -57,7 +57,7 @@ int main(int argc, char **argv) fd_set fdread; fd_set fdwrite; fd_set fdexcep; - int maxfd; + int maxfd = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); @@ -71,8 +71,10 @@ int main(int argc, char **argv) curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the - function calls, *and* you make sure that maxfd is bigger than -1 so - that the call to select() below makes sense! */ + function calls. On success, the value of maxfd is guaranteed to be + greater or equal than -1. We call select(maxfd + 1, ...), specially in + case of (maxfd == -1), we call select(0, ...), which is basically equal + to sleep. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); -- cgit v1.2.1 From bc0699f226ec55fde58a823fb818d8f8106c8fbd Mon Sep 17 00:00:00 2001 From: Constantine Sapuntzakis Date: Wed, 14 Jul 2010 00:32:53 +0200 Subject: examples: add curl_multi_timeout Make the multi-interface using examples use curl_multi_timeout to properly educate users how to do things. --- docs/examples/multi-double.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index bc5b446ea..8ac939a96 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -59,6 +59,8 @@ int main(int argc, char **argv) fd_set fdexcep; int maxfd = -1; + long curl_timeo = -1; + FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); @@ -67,6 +69,15 @@ int main(int argc, char **argv) timeout.tv_sec = 1; timeout.tv_usec = 0; + curl_multi_timeout(multi_handle, &curl_timeo); + if(curl_timeo >= 0) { + timeout.tv_sec = curl_timeo / 1000; + if(timeout.tv_sec > 1) + timeout.tv_sec = 1; + else + timeout.tv_usec = (curl_timeo % 1000) * 1000; + } + /* get file descriptors from the transfers */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); -- cgit v1.2.1 From 5fb4279ec7199e291d9bf4c903f89395f60a3d31 Mon Sep 17 00:00:00 2001 From: Dirk Manske Date: Thu, 30 Sep 2010 11:33:33 +0200 Subject: multi & hiper examples: updates and cleanups all multi and hiper examples: * don't loop curl_multi_perform calls, that was <7.20.0 style, currently the exported multi functions will not return CURLM_CALL_MULTI_PERFORM all hiper examples: * renamed check_run_count to check_multi_info * don't compare current running handle count with previous value, this was the wrong way to check for finished requests, simply call curl_multi_info_read * it's also safe to call curl_multi_remove_handle inside the curl_multi_info_read loop. ghiper.c: * replaced curl_multi_socket (that function is marked as obsolete) calls with curl_multi_socket_action calls (as in hiperfifo.c and evhiperfifo.c) ghiper.c and evhiperfifo.c: * be smart as hiperfifo.c, don't do uncessary curl_multi_* calls in new_conn and main --- docs/examples/multi-double.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 8ac939a96..702f9ae3d 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -47,8 +47,7 @@ int main(int argc, char **argv) curl_multi_add_handle(multi_handle, http_handle2); /* we start some action by calling perform right away */ - while(CURLM_CALL_MULTI_PERFORM == - curl_multi_perform(multi_handle, &still_running)); + curl_multi_perform(multi_handle, &still_running); while(still_running) { struct timeval timeout; @@ -96,8 +95,7 @@ int main(int argc, char **argv) case 0: default: /* timeout or readable/writable sockets */ - while(CURLM_CALL_MULTI_PERFORM == - curl_multi_perform(multi_handle, &still_running)); + curl_multi_perform(multi_handle, &still_running); break; } } -- cgit v1.2.1 From 18e7b52e8e95f7204d8ef7ff0f0ff0043afe45a9 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 5 Oct 2010 15:00:19 +0200 Subject: examples: use example.com in example URLs --- docs/examples/multi-double.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 702f9ae3d..3ea106bf7 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -34,7 +34,7 @@ int main(int argc, char **argv) http_handle2 = curl_easy_init(); /* set options */ - curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/"); + curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/"); /* set options */ curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/"); -- cgit v1.2.1 From 9583b4af9057c9e35ec3dd3270d4c4813b5f7aaa Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 17 Dec 2010 23:34:26 +0100 Subject: examples: fix compiler warnings --- docs/examples/multi-double.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 3ea106bf7..990fec36a 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -22,7 +22,7 @@ /* * Simply download two HTTP files! */ -int main(int argc, char **argv) +int main(void) { CURL *http_handle; CURL *http_handle2; -- cgit v1.2.1 From 1aeb635cdd296c16acb375a4a83a78f13166ccab Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 10 Mar 2011 11:48:02 +0100 Subject: sources: update source headers All C and H files now (should) feature the proper project curl source code header, which includes basic info, a copyright statement and some basic disclaimers. --- docs/examples/multi-double.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 990fec36a..120fde7b8 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -1,14 +1,24 @@ -/***************************************************************************** +/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. * - * This is a very simple example using the multi interface. - */ - + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ #include #include -- cgit v1.2.1 From 4c070de4fb01b4fbf29f8c463ba96da97b36bd2f Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Mon, 23 Jul 2012 15:34:25 +0000 Subject: examples: use do/while loop for multi examples It's conceivable that after the first time curl_multi_perform returns, the outvalue still_running will be 0, but work will have been done. This is shown by a workload of small, purely file:// based URLs. Ensure that we always read pending messages off the multi handle by forcing the while loop to run at least once. --- docs/examples/multi-double.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docs/examples/multi-double.c') diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c index 120fde7b8..91422e6e2 100644 --- a/docs/examples/multi-double.c +++ b/docs/examples/multi-double.c @@ -59,7 +59,7 @@ int main(void) /* we start some action by calling perform right away */ curl_multi_perform(multi_handle, &still_running); - while(still_running) { + do { struct timeval timeout; int rc; /* select() return code */ @@ -108,7 +108,7 @@ int main(void) curl_multi_perform(multi_handle, &still_running); break; } - } + } while(still_running); curl_multi_cleanup(multi_handle); -- cgit v1.2.1