CURLOPT_TCP_KEEPALIVE(3)

πŸ’– CURLOPT_TCP_KEEPALIVE β€” TCP keep-alive probing πŸ’–

okay bestie, this is a page from the libcurl manual, specifically CURLOPT_TCP_KEEPALIVE(3). yes, the all-caps name is a lot, but she's iconic, we move.


✨ NAME ✨

CURLOPT_TCP_KEEPALIVE β€” literally just TCP keep-alive probing. that's it. that's the whole vibe 🫢

🌸 SYNOPSIS 🌸

#include <curl/curl.h>

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_TCP_KEEPALIVE, long probe);

πŸ’… DESCRIPTION πŸ’…

so you pass a long. that's the move. πŸŽ€

  • if you set it to 1, TCP keepalive probes are used. like, your connection is out there poking the other side going "hey bestie, u still there?? πŸ‘€"
  • the delay and frequency of those probes can be controlled by the CURLOPT_TCP_KEEPIDLE(3), CURLOPT_TCP_KEEPINTVL(3), and CURLOPT_TCP_KEEPCNT(3) options β€” provided the operating system supports them. that caveat is doing heavy lifting ngl, some OSes just don't wanna πŸ’€
  • set it to 0 (this is the default behavior) to disable keepalive probes. dead it. no probes. ✨

πŸ¦‹ DEFAULT πŸ¦‹

0

like, off. no cap. she's chill by default.

🌈 PROTOCOLS 🌈

This functionality affects tcp only. just tcp. nobody else invited to this party πŸ™ˆ

⭐ EXAMPLE ⭐

this is the whole thing, verbatim, no edits, she's beautiful:

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);

    /* 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);
  }
}

look at her go. 1L turns it on, 120L idle seconds, 60L between probes, 3L max probes. ate. πŸ”₯

πŸŽ€ AVAILABILITY πŸŽ€

Added in curl 7.25.0. vintage-ish. still slays.

πŸ’œ RETURN VALUE πŸ’œ

curl_easy_setopt(3) returns a CURLcode indicating success or error. that's the messenger, she tells you how it went.

  • CURLE_OK (0) means everything was OK. we love a zero. 🫢
  • non-zero means an error occurred β€” see libcurl-errors(3) for the full breakdown of what went wrong.
lowkey important: don't ignore a non-zero return, bestie, that's your program telling you something's up 😭

🧚 SEE ALSO 🧚

go read those too, they're the friend group, they come as a set πŸ’–


and that's the page!! brought to you by libcurl, dated 2026-10-12, and originally filed under CURLOPT_TCP_KEEPALIVE(3). mwah πŸ’‹ go keep those TCP connections alive, queen πŸ‘‘βœ¨

πŸ“¦ where this page comes from