CURL_MULTI_SOCKET_ACTION(3)
π curl_multi_socket_action(3) π
okay bestie, welcome to the Library Functions Manual page for curl_multi_socket_action(3) β this is the βsocket said something, now what?β moment of your libcurl era. β¨
πΈ NAME πΈ
curl_multi_socket_action β read/write available data given an action. no cap. π
β¨ SYNOPSIS β¨
#include <curl/curl.h>
CURLMcode curl_multi_socket_action(CURLM *multi_handle,
curl_socket_t sockfd,
int ev_bitmask,
int *running_handles);
π¦ DESCRIPTION π¦
When your app has already clocked action on a socket that libcurl is handling, it should call curl_multi_socket_action(3) with the sockfd argument set to the socket that had the action. π
If the events on a socket are known, they can be passed as an events bitmask ev_bitmask β first set ev_bitmask to 0, then add using bitwise OR (|) any combination of events chosen from CURL_CSELECT_IN, CURL_CSELECT_OUT, or CURL_CSELECT_ERR. When the events on a socket are unknown, pass 0 instead, and libcurl tests the descriptor internally. It is also permissible to pass CURL_SOCKET_TIMEOUT to the sockfd parameter in order to initiate the whole process or when a timeout occurs. β°
At return, running_handles points to the number of running easy handles within the multi handle. When this number reaches zero, all transfers are complete/done. But listen: when you call curl_multi_socket_action(3) on a specific socket and the counter decreases by one, it DOES NOT necessarily mean that this exact socket/transfer is the one that completed. Use curl_multi_info_read(3) to figure out which easy handle that completed. π
The curl_multi_socket_action(3) function informs the application about updates in the socket (file descriptor) status by doing none, one, or multiple calls to the socket callback function set with the CURLMOPT_SOCKETFUNCTION(3) option to curl_multi_setopt(3). They update the status with changes since the previous time the callback was called. π
Get the timeout time by setting the CURLMOPT_TIMERFUNCTION(3) option with curl_multi_setopt(3). Your application then gets called with information on how long to wait for socket actions at most before doing the timeout action: call the curl_multi_socket_action(3) function with the sockfd argument set to CURL_SOCKET_TIMEOUT. You can also use the curl_multi_timeout(3) function to poll the value at any given time, but for an event-based system using the callback is far better than relying on polling the timeout value. π
β οΈ bestie, this is important: when this function returns error, the state of all transfers are uncertain and they cannot be continued. curl_multi_socket_action(3) should not be called again on the same multi handle after an error has been returned, unless first removing all the handles and adding new ones. like, fully reset the situation. π
π TYPICAL USAGE π
- Create a multi handle. π§Έ
- Set the socket callback with
CURLMOPT_SOCKETFUNCTION(3). - Set the timeout callback with
CURLMOPT_TIMERFUNCTION(3), to get to know what timeout value to use when waiting for socket activities. - Add easy handles with
curl_multi_add_handle(). - Provide some means to manage the sockets libcurl is using, so you can check them for activity. This can be done through your application code, or by way of an external library such as
libeventorglib. - Call
curl_multi_socket_action(..., CURL_SOCKET_TIMEOUT, 0, ...)to kickstart everything. To get one or more callbacks called. - Wait for activity on any of libcurl's sockets, use the timeout value your callback has been told.
- When activity is detected, call
curl_multi_socket_action()for the socket(s) that got action. If no activity is detected and the timeout expires, callcurl_multi_socket_action(3)withCURL_SOCKET_TIMEOUT.
π PROTOCOLS π
This functionality affects all supported protocols. periodt. π«Ά
π EXAMPLE π
int main(void)
{
/* the event-library gets told when there activity on the socket 'fd',
which we translate to a call to curl_multi_socket_action() */
int running = 0;
int fd = 3; /* the descriptor that had action */
int bitmask = 2; /* what activity that happened */
CURLM *multi = curl_multi_init();
CURLMcode mresult = curl_multi_socket_action(multi, fd, bitmask, &running);
if(mresult)
printf("error: %s\n", curl_multi_strerror(mresult));
}
β AVAILABILITY β
Added in curl 7.15.4. π
π 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_fdset(3)curl_multi_info_read(3)curl_multi_init(3)thehiperfifo.cexample
mwah π β signed with love by source/version: libcurl, dated 2026-10-12. now go be the main character of your event loop! π«Ά
π¦ 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