CURLOPT_TCP_KEEPCNT(3)

πŸ’… CURLOPT_TCP_KEEPCNT(3) πŸ’…

ok so this is a page straight out of the libcurl Library Functions Manual, and it's about CURLOPT_TCP_KEEPCNT β€” the maximum number of TCP keep-alive probes. yes bestie, we're getting into how many times your socket gets to poke the server before it gives up and ghosts. 🫢


✨ NAME ✨

CURLOPT_TCP_KEEPCNT β€” maximum number of TCP keep-alive probes, like, ever πŸ”₯


🌸 SYNOPSIS 🌸

#include <curl/curl.h>

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_TCP_KEEPCNT, long cnt);

πŸ¦‹ DESCRIPTION πŸ¦‹

so you pass a long, okay? that's the whole vibe. it sets the number of probes to send before dropping the connection. literally just "how many times do we tap the server on the shoulder before we're like, ok bye". πŸ’€

super important, no cap: not all operating systems support this option. so if it's not doing anything on your box, that's the OS being a hater, not you. πŸ’”

this whole thing was added in 8.9.0, so if you're on something older it's giving nonexistent.

the maximum value this option accepts is INT_MAX or whatever your system allows. any larger value is capped to this amount β€” so like, don't try to be cute with a giant number, it's not gonna happen. πŸ™ˆ


πŸŽ€ DEFAULT πŸŽ€

9

nine. iconic. periodt. πŸ‘‘


🌈 PROTOCOLS 🌈

this functionality affects tcp only β€” tcp girlies only, everyone else can see themselves out. πŸ’…


⭐ EXAMPLE ⭐

ok here's the whole thing, copy it verbatim, don't touch my code:

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

    /* enable TCP keep-alive for this transfer */
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

    /* set keep-alive idle time to 120 seconds */
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);

    /* interval time between keep-alive probes: 60 seconds */
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L);

    /* maximum number of keep-alive probes: 3 */
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPCNT, 3L);

    curl_easy_perform(curl);
  }
}

πŸ’– AVAILABILITY πŸ’–

added in curl 8.9.0 β€” baby of the keep-alive family, lowkey the newest member. 🍼


🧚 RETURN VALUE 🧚

curl_easy_setopt(3) returns a CURLcode indicating success or error. that's the whole tea.

  • CURLE_OK (which is 0) means everything was OK β€” slay. βœ…
  • non-zero means an error occurred β€” not slay. πŸ’€

if you got the non-zero one, go see libcurl-errors(3) and it'll tell you what went wrong, iykyk.


πŸ‘‘ SEE ALSO πŸ‘‘

the whole keep-alive friend group, go say hi to all of them 🫢


ok that's the page!! this beauty is brought to you by libcurl, dated 2026-10-12, and honestly? she ate. now go forth and keep those connections alive, bestie πŸ¦‹βœ¨πŸ’œ

πŸ“¦ where this page comes from