CURLINFO_ACTIVESOCKET(3)

πŸ’… CURLINFO_ACTIVESOCKET(3) πŸ’…

okay so real talk β€” this is a page from the libcurl manual, and it's about getting your hands on the actual socket. bestie, it's lowkey iconic. let's go 🫢


✨ NAME ✨

CURLINFO_ACTIVESOCKET β€” the active socket, periodt πŸ’…


✨ SYNOPSIS ✨

#include <curl/curl.h>

CURLcode curl_easy_getinfo(CURL *handle, CURLINFO_ACTIVESOCKET,
                           curl_socket_t *socket);

classic move: you pass in the handle, the info you want (CURLINFO_ACTIVESOCKET), and a pointer so it can hand you back the good stuff πŸŽ€


✨ DESCRIPTION ✨

So here's the vibe: you pass a pointer to a curl_socket_t, and it gets filled in with the most recently active socket that this curl session used for the transfer connection. That's it. That's the whole girlboss moment ✨

ngl, one thing to know β€” if the socket is no longer valid, you get CURL_SOCKET_BAD back. not great, not terrible, just how it is πŸ™ˆ

🚨 okay this part matters, don't skip it: when you are finished working with the socket, you must call curl_easy_cleanup(3) as usual on the easy handle, and let libcurl close the socket and clean up the other resources associated with the handle. do not go closing it yourself, bestie. that's libcurl's job. iykyk.

also, heads up β€” this option returns the active socket only after the transfer is complete. so it's typically used in combination with CURLOPT_CONNECT_ONLY(3), which skips the transfer phase entirely. honestly? that combo ate. πŸ”₯

and some lore for you 🧚: CURLINFO_ACTIVESOCKET(3) was added as a replacement for CURLINFO_LASTSOCKET(3), since that one just straight up does not work on all platforms. so like... use the new one. πŸ’€


🌈 PROTOCOLS 🌈

this functionality affects all supported protocols πŸ¦‹ (yes, all of them, she's versatile)


🌸 EXAMPLE 🌸

here's the whole thing, and yes you can literally copy it:

int main(void)
{
  CURL *curl = curl_easy_init();
  if(curl) {
    CURLcode result;
    curl_socket_t sockfd;
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");

    /* Do not do the transfer - only connect to host */
    curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
    result = curl_easy_perform(curl);
    if(result != CURLE_OK) {
      printf("Error: %s\n", curl_easy_strerror(result));
      curl_easy_cleanup(curl);
      return 1;
    }

    /* Extract the socket from the curl handle */
    result = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
    if(!result && sockfd != CURL_SOCKET_BAD) {
      /* operate on sockfd */
    }

    curl_easy_cleanup(curl);
  }
}

like, look at her go 😭 connection only, no transfer, socket extracted, main character energy the entire time πŸ‘‘


⭐ AVAILABILITY ⭐

Added in curl 7.45.0 β€” she's been around, she's established, she's got seniority πŸ’œ


πŸŽ€ RETURN VALUE πŸŽ€

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

  • CURLE_OK (0) means everything was OK ✨
  • non-zero means an error occurred 😭 β€” see libcurl-errors(3) for the full tea

πŸ¦‹ SEE ALSO πŸ¦‹

the whole squad, fr fr 🫢


and that's the page!! this cutie came from the libcurl manual, dated 2026-10-12, and the page you're reading is CURLINFO_ACTIVESOCKET(3). go get that socket bestie, you got this πŸ’–πŸŽ€βœ¨

πŸ“¦ where this page comes from