CURLMOPT_SOCKETFUNCTION(3)
π CURLMOPT_SOCKETFUNCTION(3) π
okay so bestie, welcome to the CURLMOPT_SOCKETFUNCTION(3) page β it's a libcurl library functions manual situation, and we're about to make your socket-watching era iconic β¨
π NAME π
CURLMOPT_SOCKETFUNCTION β the callback that gets told what to wait for π
πΈ SYNOPSIS πΈ
#include <curl/curl.h>
int socket_callback(CURL *easy, /* easy handle */
curl_socket_t s, /* socket */
int what, /* describes the socket */
void *clientp, /* private callback pointer */
void *socketp); /* private socket pointer */
CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_SOCKETFUNCTION, socket_callback);
π¦ DESCRIPTION π¦
So you pass in a pointer to your callback function, and it has to match that prototype up there β¬οΈ β no freestyling the signature, ok?
When curl_multi_socket_action(3) gets called, it uses this callback to keep your app in the loop about updates in the socket (file descriptor) status β and it does that by making none, one, or multiple calls to socket_callback π. Your callback gets status updates covering what's changed since the last time it was called. And if you set that callback pointer to NULL, no callback happens at all. peace out βοΈ
Then libcurl is like "okay bestie, you're on socket duty now" β it expects the application to monitor the sockets for whatever specific activities, and tell libcurl again when something happens on one of them. You tell libcurl by calling curl_multi_socket_action(3). periodt π«Ά
Heads up: this callback may get invoked at any time when interacting with libcurl π. It may even happen after all transfers are done, and it's likely to happen during a call to curl_multi_cleanup(3) when cached connections are shut down. so don't be surprised, it's giving normal π
β¨ CALLBACK ARGUMENTS β¨
easyβ identifies the specific transfer that this update is related to. But since this callback manages a whole multi handle, your app should not make assumptions about which particular handle gets passed here π. It might even be an internal easy handle that your application didn't add itself. main character energy from a handle you've never met fr fr.sβ the specific socket this function invocation concerns. If thewhatargument is notCURL_POLL_REMOVE, then it holds info about what activity on this socket the application is supposed to monitor π. And subsequent calls to this callback might update thewhatbits for a socket that's already being monitored. lowkey convenient.- return value β the socket callback should return
0on success, and-1on error π. And if this callback returns error, all transfers currently in progress in this multi handle are aborted and made to fail. so like... no pressure but also all the pressure π« clientpβ set withCURLMOPT_SOCKETDATA(3).socketpβ set withcurl_multi_assign(3)orNULL.
The what parameter informs the callback on the status of the given socket π. It can hold one of these values:
CURL_POLL_INβ wait for incoming data. For the socket to become readable π₯CURL_POLL_OUTβ wait for outgoing data. For the socket to become writable π€CURL_POLL_INOUTβ wait for incoming and outgoing data. For the socket to become readable or writable πCURL_POLL_REMOVEβ the specified socket/file descriptor is no longer used by libcurl for any active transfer. It might soon be added again π (it's giving "not goodbye, just see you later")
π DEFAULT π
NULL (no callback) β so like, nothing happens unless you ask π
π PROTOCOLS π
This functionality affects all supported protocols β every single one, no cap π
π EXAMPLE π
struct priv {
void *ours;
};
static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
{
struct priv *p = cbp;
printf("our ptr: %p\n", p->ours);
if(what == CURL_POLL_REMOVE) {
/* remove the socket from our collection */
}
if(what & CURL_POLL_IN) {
/* wait for read on this socket */
}
if(what & CURL_POLL_OUT) {
/* wait for write on this socket */
}
return 0;
}
int main(void)
{
struct priv setup;
CURLM *multi = curl_multi_init();
/* ... use socket callback and custom pointer */
curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, &setup);
}
β AVAILABILITY β
Added in curl 7.15.4 β she's been around a minute, an OG π«Ά
π₯ RETURN VALUE π₯
Returns CURLM_OK. that's it, that's the whole vibe π
π§ SEE ALSO π§
okay that's the whole page bestie, go monitor some sockets π
this has been libcurl, dated 2026-10-12 β catch you in the next manpage, mwah πβ¨
π¦ 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