cURL / Mailing Lists / curl-library / Single Mail

curl-library

help with multi and write_data()

From: Jacob Hallenborg <cl9jhall_at_cling.gu.se>
Date: Wed, 12 Mar 2003 21:59:26 +0100

Hi,

I would be glad for some help with the following code. The problem is
that the execution never reaches the write_data() function. It worked
fine when I used the easy interface.
I'm using curl 7.10.4-pre3 (i686-pc-linux-gnu) RedHat 8.

Thanks in advance.
/jacob

#include <iostream>
#include <sstream>
#include <curl/curl.h>

using namespace std;

void fetch_urls(char **urls, int size);
size_t write_data(void *buffer, size_t size, size_t nmemb, void *stream);

int timeout=10;

void fetch_urls(char **urls, int size) {
   int still_running;
   CURL *curl_handles[size];
   stringstream *stream_list[size];
   CURLM *curlm_handle = curl_multi_init();

   if(!curlm_handle) {
     cout << "Error in curlm_handle!!\n";
     exit(1);
   }

   for (int i=0; i<size; i++) {
     curl_handles[i] = curl_easy_init();
     if(!curl_handles) {
        cout << "Error in curl_handles!!\n";
        exit(1);
     }
     stream_list[i] = new stringstream;
     curl_easy_setopt(curl_handles[i], CURLOPT_TIMEOUT, timeout);
     curl_easy_setopt(curl_handles[i], CURLOPT_URL, urls[i]);
     curl_easy_setopt(curl_handles[i], CURLOPT_WRITEFUNCTION,

       write_data);
     curl_easy_setopt(curl_handles[i], CURLOPT_FILE, stream_list[i]);
     curl_multi_add_handle(curlm_handle, curl_handles[i]);
   }

   while(CURLM_CALL_MULTI_PERFORM ==
      curl_multi_perform(curlm_handle, &still_running)) {
     cout << "still_runnning: " << still_running << endl;
   }

   curl_multi_cleanup(curlm_handle);

   for (int i=0; i<size; i++) {
     curl_easy_cleanup(curl_handles[i]);
   }
}

size_t write_data(void *buffer, size_t size, size_t nmemb, void *stream) {
   cout << "got here\n"; // it never gets here!!!
   (*(ostringstream*) stream) << (char *)buffer;
   return nmemb*size;
}

int main(int argc, char** argv) {
   char *urls[] = {"www.dn.se", "www.gu.se", "www.cnn.com"};
   fetch_urls(urls, 3);
   return 0;
}

-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open!
Get cracking and register here for some mind boggling fun and
the chance of winning an Apple iPod:
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
Received on 2003-03-12