cURL / Mailing Lists / curl-library / Single Mail

curl-library

curl_multi_fdset always returns -1 for maxfd

From: Vishakha Vaidya <vvaidya_at_adobe.com>
Date: Tue, 11 Nov 2014 14:40:48 +0000

Hi,

Following code has been taken from example multi-single.c and modified to download a file from ftp server.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* curl stuff */
#include <curl/curl.h>
#include <Winsock2.h>

struct FtpFile {
const char *filename;
FILE *stream;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream) {
/* open file for writing */
out->stream=fopen(out->filename, "wb");
if(!out->stream)
return -1; /* failure, can't open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}

/*
Simply download two HTTP files!
*/
int main(void)
{
CURL *http_handle;
CURL *http_handle2;
CURLM *multi_handle;
struct FtpFile ftpfile={
"test.html", /* name to store the file as if succesful */
NULL
};
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, "ftp://<ftp server name>/ test.html");
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(http_handle, CURLOPT_WRITEFUNCTION, my_fwrite);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(http_handle, CURLOPT_WRITEDATA, &ftpfile);

/* init a multi stack */
multi_handle = curl_multi_init();

/* add the individual transfers */
curl_multi_add_handle(multi_handle, http_handle);

/* we start some action by calling perform right away */
curl_multi_perform(multi_handle, &still_running);

do {
struct timeval timeout;
int rc; /* select() return code */

fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;

long curl_timeo = -1;

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;

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);

/* In a real-world program you OF COURSE check the return code of the
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);

switch(rc) {
case -1:
/* select error */
//break;
case 0:
default:
/* timeout or readable/writable sockets */
curl_multi_perform(multi_handle, &still_running);
break;
}
} while(still_running);

curl_multi_cleanup(multi_handle);

curl_easy_cleanup(http_handle);
curl_easy_cleanup(http_handle2);

printf("DONE");
getch();
return 0;
}

Maxfd in curl_multi_fdset always returns -1 and the do while loop never exits.

This code was working in 7.22.0, I upgraded to 7.38.0 and it has stopped working. Am I missing something?

Thanks and regards,
Vishakha

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2014-11-11