CURLOPT_PORT(3)

πŸ’… CURLOPT_PORT β€” remote port number to connect to

ok so this is from the Library Functions Manual, page CURLOPT_PORT(3), and bestie… we gotta talk about it. 🫢


✨ SYNOPSIS ✨

#include <curl/curl.h>

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_PORT, long number);

🌸 DESCRIPTION 🌸

Okay so real talk first: we lowkey discourage using this option πŸ’€ because its scope is genuinely not obvious and honestly hard to predict. The move? Set the preferred port number in the URL instead. That's the vibe. πŸŽ€

So what does it even do β€” this option sets number to be the remote port number to connect to, instead of the one specified in the URL or the default port for the protocol you're using. it's giving override energy. πŸ‘‘

Usually you just let the URL decide which port to use, but this lets the application override that. main character option, fr fr. ✨

⚠️ Now the important bit, bestie, don't skip this: while this option accepts a long, a port number is an unsigned 16 bit number β€” so using a port number lower than zero or over 65535 causes a CURLE_BAD_FUNCTION_ARGUMENT error. no cap, it will simply not slay. 😭

πŸ¦‹ DEFAULT πŸ¦‹

0, which makes it not used. Which also means port number zero is literally impossible to set with this API. πŸ™ˆ


🌈 PROTOCOLS 🌈

This functionality affects all supported protocols. every single one. πŸ’…


πŸ’– EXAMPLE πŸ’–

int main(void)
{
  CURL *curl = curl_easy_init();
  if(curl) {
    CURLcode result;
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/foo.bin");
    curl_easy_setopt(curl, CURLOPT_PORT, 8080L);
    result = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
}

look at her go 🧚 8080L β€” yes with the L, she's a long. ⭐


πŸŽ€ AVAILABILITY πŸŽ€

Added in curl 7.1. vintage icon behavior. πŸ‘‘


πŸ”₯ RETURN VALUE πŸ”₯

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

CURLE_OK (0) means everything was OK β€” we love to see it. non-zero means an error occurred, so go check libcurl-errors(3). πŸ’œ


🌸 SEE ALSO 🌸

go read those too, iykyk πŸ’…


ok bye bestie, this page came from libcurl, dated 2026-10-12, filed under CURLOPT_PORT(3) β€” mwah, slay, periodt. πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from