CURLOPT_CLOSESOCKETDATA(3)

πŸ’– NAME πŸ’–

okay bestie, welcome to the CURLOPT_CLOSESOCKETDATA(3) page from the Library Functions Manual β€” this one is all about the pointer passed to the socket close callback. slay, very niche, very useful πŸ’…


✨ SYNOPSIS ✨

       #include <curl/curl.h>

       CURLcode curl_easy_setopt(CURL *handle, CURLOPT_CLOSESOCKETDATA,
                                 void *pointer);

πŸ¦‹ DESCRIPTION πŸ¦‹

So you pass in a pointer, right? And libcurl literally leaves it untouched β€” no cap, it does not mess with it. Then libcurl passes that same pointer as the first argument in the closesocket callback that you set with CURLOPT_CLOSESOCKETFUNCTION(3). it’s giving β€œcustom data, but make it socket-close flavored” πŸŽ€

⚠️ heads up bestie: when you’re using multi/share handles, your callback may get invoked even after the easy handle has been cleaned up. The callback and data are inherited by a new connection, and that connection may live longer than the transfer itself in the multi/share handle’s connection cache. iykyk 😭

πŸ’€ DEFAULT πŸ’€

NULL

like, nothing by default. no data, no drama 🫢


🌈 PROTOCOLS 🌈

This functionality affects all supported protocols. every single one, periodt ✨


πŸ“ EXAMPLE πŸ“

       struct priv {
         void *custom;
       };

       static int closesocket(void *clientp, curl_socket_t item)
       {
         struct priv *my = clientp;
         printf("our ptr: %p\n", my->custom);

         printf("libcurl wants to close %d now\n", (int)item);
         return 0;
       }

       int main(void)
       {
         struct priv myown;
         CURL *curl = curl_easy_init();

         /* call this function to close sockets */
         curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, closesocket);
         curl_easy_setopt(curl, CURLOPT_CLOSESOCKETDATA, &myown);

         curl_easy_perform(curl);
         curl_easy_cleanup(curl);
       }

πŸ‘‘ AVAILABILITY πŸ‘‘

Added in curl 7.21.7 β€” vintage but iconic πŸ’œ


πŸ”₯ RETURN VALUE πŸ”₯

curl_easy_setopt(3) returns a CURLcode indicating success or error.

  • CURLE_OK (0) means everything was OK β€” we love a zero πŸ’–
  • non-zero means an error occurred, so go see libcurl-errors(3) bestie 😭

🧚 SEE ALSO 🧚

go read those too, they’re the friend group of this page 🌸


mwah πŸ’‹ β€” source/version: libcurl Β· date: 2026-10-12 Β· this little icon has been served fresh πŸŽ€

πŸ“¦ where this page comes from