curl_multi_fdset(3)
π curl_multi_fdset(3) β straight from the libcurl Library Functions Manual, bestie
β¨ NAME β¨
curl_multi_fdsetβ extract file descriptor information from a multi handle π«Ά
π SYNOPSIS π
#include <curl/curl.h>
CURLMcode curl_multi_fdset(CURLM *multi_handle,
fd_set *read_fd_set,
fd_set *write_fd_set,
fd_set *exc_fd_set,
int *max_fd);
π DESCRIPTION π
Okay so this function is literally how you extract file descriptor information from a given multi_handle. libcurl hands you back its own fd_set sets. Your app can use those to select() on β iconic β but bestie, please be sure to FD_ZERO them before calling this function, because curl_multi_fdset(3) only adds its own descriptors. It does not zero or otherwise remove any others. And curl_multi_perform(3) should be called as soon as one of them is ready to be read from or written to. Periodt. π
The read_fd_set argument should point to an object of type fd_set that on return specifies the file descriptors to be checked for being ready to read. π
The write_fd_set argument should point to an object of type fd_set that on return specifies the file descriptors to be checked for being ready to write. β¨
The exc_fd_set argument should point to an object of type fd_set that on return specifies the file descriptors to be checked for error conditions. π¨
If no file descriptors are set by libcurl, max_fd contains -1 when this function returns. Otherwise it contains the highest descriptor number libcurl set. When libcurl returns -1 in max_fd, it is because libcurl currently does something that is not possible for your application to monitor with a socket β and unfortunately you can then not know exactly when the current action is completed using select(). π You then need to wait a while before you proceed and call curl_multi_perform(3) anyway. How long to wait? Unless curl_multi_timeout(3) gives you a lower number, we suggest 100 milliseconds or so, but you may want to test it out in your own particular conditions to find a suitable value. π§
When doing select(), you should use curl_multi_timeout(3) to figure out how long to wait for action. Call curl_multi_perform(3) even if no activity has been seen on the fd_sets after the timeout expires, as otherwise internal retries and timeouts may not work as you would think and want. π₯
β οΈ If one of the sockets used by libcurl happens to be larger than what can be set in anfd_setβ which on POSIX systems means that the file descriptor is larger thanFD_SETSIZEβ then libcurl tries to not set it. Setting a too large file descriptor in anfd_setimplies an out of bounds write which can cause crashes, or worse. π The effect of NOT storing it might possibly save you from the crash, but makes your program NOT wait for sockets it should wait for...
π PROTOCOLS π
This functionality affects all supported protocols β¨
π¦ EXAMPLE π¦
int main(void)
{
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd;
int rc;
CURLMcode mresult;
struct timeval timeout = { 1, 0 };
CURLM *multi = curl_multi_init();
do {
/* call curl_multi_perform() */
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* get file descriptors from the transfers */
mresult = curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
if(mresult != CURLM_OK) {
fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mresult);
break;
}
/* wait for activity on one of the sockets */
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
} while(!mresult);
}
πΈ AVAILABILITY πΈ
Added in curl 7.9.6 π
β RETURN VALUE β
This function returns a CURLMcode indicating success or error.
CURLM_OK (0) means everything was OK, non-zero means an error occurred, see libcurl-errors(3). π«Ά
π SEE ALSO π
curl_multi_cleanup(3)curl_multi_init(3)curl_multi_perform(3)curl_multi_timeout(3)curl_multi_wait(3)curl_multi_waitfds(3)select(2)
With love from your libcurl bestie π β source/version: libcurl, date: 2026-10-12 π
π¦ where this page comes from
- fedora 44: libcurl-devel 8.18.0-14.fc44 Β· curl
- fedora 44: libcurl-devel 8.18.0-14.fc44 Β· curl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms