What I want to do: download multiple files at the same time.
Issue: curl_multi_fdset
always return 1.
Issue 2: I don't really understand how fd_set work. I am new to C and the doc is quite confusing.
Sources: I used this example and this progress bar.
My full .cpp: Github
So the problem seems to be in the initializing method:
static void initialize(CURLM *cm, int i, char* url[], std::string filepath)
{
CURL *eh = curl_easy_init();
FILE *fp; // added to the example
fp = fopen(filepath.c_str(), "wb"); // added to the example
curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, callback);
curl_easy_setopt(eh, CURLOPT_WRITEDATA, fp); // added to the example
curl_easy_setopt(eh, CURLOPT_NOPROGRESS, 0); // added to the example
curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
curl_easy_setopt(eh, CURLOPT_URL, url[i]);
curl_easy_setopt(eh, CURLOPT_PRIVATE, url[i]);
curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);
curl_easy_setopt(eh, CURLOPT_PROGRESSFUNCTION, progress_func); // added to the example
curl_multi_add_handle(cm, eh);
}
However, it also won't work without these additional options. So it has to be something different but I didn't change anything regarding the fd_set. Any help is appreciated.
Call of initialize:
CURLM *cm = nullptr;
CURLMsg *msg;
long L;
unsigned int C = 0;
int M, Q, U = -1;
fd_set R, W, E;
struct timeval T;
curl_global_init(CURL_GLOBAL_ALL);
/* we can optionally limit the total amount of connections this multi handle uses */
curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long) MAX);
if ((argc - 2) < MAX)
{
for (C = 0; C < argc - 2; ++C)
{
path = "test" + std::to_string(C) + ".html";
initialize(cm, C, urls, path);
}
}
Call of the function which returns 1:
FD_ZERO(&R);
FD_ZERO(&W);
FD_ZERO(&E);
if (curl_multi_fdset(cm, &R, &W, &E, &M))
{
fprintf(stderr, "E: curl_multi_fdset\n");
return EXIT_FAILURE;
}