DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk5 min

setsockopt(2): Set Socket Options on Linux and POSIX Systems

A practical reference to setsockopt(): how its five arguments map to protocol levels and option values, when to set common socket and TCP options, and how to diagnose failures.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

setsockopt() changes an option on an existing socket. You identify the socket with sockfd, choose the protocol level and optname, point optval at a correctly typed value, and pass its byte count in optlen. Generic options use SOL_SOCKET; TCP-specific options use IPPROTO_TCP. A successful call returns 0; failure returns -1 and sets errno.

Function signature and the five inputs

On Linux, the prototype is:

int setsockopt(int sockfd, int level, int optname,
               const void *optval, socklen_t optlen);
  1. sockfd: a file descriptor returned by socket(). It must refer to a socket, not an ordinary file.
  2. level: the protocol layer that owns the option. Use SOL_SOCKET for socket-layer options, IPPROTO_TCP for TCP options, and the relevant IP or IPv6 level for network-layer options.
  3. optname: the specific option, such as SO_REUSEADDR or TCP_NODELAY.
  4. optval: a pointer to the option value. Many switches use an int, but some options require a structure, string, file descriptor, or protocol-specific buffer.
  5. optlen: the number of bytes available at optval. Pass sizeof(value) for an object, not the size of the pointer.

For the common Boolean socket options on Linux, a nonzero integer enables the option and zero disables it. The option’s own manual page defines the required representation and length; do not assume every option accepts an int.

Choosing the protocol level

Option family Typical level Examples Timing and trade-off Portability
Generic socket behavior SOL_SOCKET SO_REUSEADDR, SO_REUSEPORT, SO_KEEPALIVE, SO_RCVBUF, SO_SNDTIMEO Binding, buffering, timeouts, liveness, close behavior, filtering and metadata; timing depends on the individual option Many are widely available, but exact semantics and names vary by Unix system
TCP behavior IPPROTO_TCP TCP_NODELAY, TCP_CORK, TCP_USER_TIMEOUT Latency versus batching, congestion behavior, or failure-detection limits Several are Linux-specific or explicitly non-portable
IPv4 or IPv6 behavior IPPROTO_IP or IPPROTO_IPV6 Protocol-specific IP options Address-family and packet-processing behavior Check the target platform’s protocol manual

An option at the wrong level commonly fails with ENOPROTOOPT. The numeric value of an option name is not enough; its owning protocol level is part of the API contract.

Generic SOL_SOCKET examples

Allowing address reuse

int enabled = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
               &enabled, sizeof enabled) == -1) {
    /* inspect errno */
}

Set this before bind() when the program needs the platform’s SO_REUSEADDR behavior for rebinding. SO_REUSEPORT is a separate option with separate kernel rules; do not treat it as an alias.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Enabling keepalive

int enabled = 1;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
               &enabled, sizeof enabled) == -1) {
    /* inspect errno */
}

SO_KEEPALIVE enables liveness probing. On Linux, TCP-specific probe intervals and counts are configured separately with TCP_KEEPIDLE, TCP_KEEPINTVL, and TCP_KEEPCNT at IPPROTO_TCP.

Buffers and timeouts

SO_RCVBUF and SO_SNDBUF control socket buffering, affecting memory use and the amount of data that can be in flight. SO_RCVTIMEO and SO_SNDTIMEO use a platform-defined time-value structure rather than a Boolean integer; pass the exact structure and length required by the target system.

Other socket-layer facilities

  • SO_BROADCAST permits broadcast transmission where supported.
  • SO_LINGER changes close behavior and requires a linger structure.
  • SO_ATTACH_FILTER and SO_ATTACH_BPF attach packet filters; classic BPF support is documented from Linux 2.2 and extended BPF attachment from Linux 3.19.
  • SO_ACCEPTCONN is read-only: it reports whether listen() has marked the socket as listening, so it is queried with getsockopt(), not set with setsockopt().

TCP options at IPPROTO_TCP

TCP_NODELAY: send small segments promptly

int enabled = 1;
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
               &enabled, sizeof enabled) == -1) {
    /* inspect errno */
}

This disables Nagle buffering for the connection, allowing small writes to be sent without that batching delay. It changes a latency-versus-packet-efficiency trade-off; the manual describes the mechanism, not a universal performance improvement.

TCP_CORK: batch partial frames

TCP_CORK holds partial frames so an application can batch writes. Linux documents a 200-millisecond ceiling for the hold. Use it when deliberate aggregation is more important than immediate delivery, and clear it when the batch is complete.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keepalive tuning

After enabling SO_KEEPALIVE, Linux TCP exposes TCP_KEEPIDLE, TCP_KEEPINTVL, and TCP_KEEPCNT for idle delay, probe interval, and probe count. These are separate integer options and should be set at IPPROTO_TCP.

Timeout and receive-window controls

  • TCP_USER_TIMEOUT bounds how long a synchronized connection may remain without successful end-to-end progress. A shorter bound detects failure sooner but tolerates less delay.
  • TCP_WINDOW_CLAMP limits the advertised receive window.
  • TCP_CONGESTION selects a per-socket congestion-control algorithm, subject to the algorithms allowed by the system and any required privilege.
  • TCP_DEFER_ACCEPT controls when a listening socket is awakened, allowing the listener to wait for more than the initial connection handshake.

Lifecycle: when to set an option

Option timing is option-specific. Binding-related settings such as SO_REUSEADDR are normally applied before bind(). Listener behavior such as TCP_DEFER_ACCEPT belongs on the listening socket before or around listen(), according to the platform’s documentation. Connection behavior such as TCP_NODELAY can be changed on an established TCP socket. When accepted sockets are created, verify whether the desired setting must be applied again to each connected descriptor.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Error handling and diagnosis

Check the return value immediately and preserve errno while reporting the failure:

if (setsockopt(fd, level, name, &value, sizeof value) == -1) {
    perror("setsockopt");
}
  • EBADF: sockfd is not a valid file descriptor.
  • ENOTSOCK: the descriptor is valid but does not refer to a socket.
  • EFAULT: optval points to inaccessible memory.
  • EINVAL: the length, value, or other argument is invalid for that option.
  • ENOPROTOOPT: the option is unknown at the selected protocol level, or unsupported by that protocol.

For a persistent EINVAL, verify the option’s required type, exact optlen, permitted range, socket state, and whether the option is settable rather than read-only. For ENOPROTOOPT, check both the level and the running kernel or operating system; an option documented for Linux may not exist on another Unix system.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value

Portability and privilege

setsockopt() is standardized by POSIX.1-2024 and has roots in POSIX.1-2001, SVr4, 4.4BSD, and 4.2BSD. The portable interface does not make every option portable. Linux’s TCP documentation identifies several options that should not be used in code intended for other systems. Isolate non-portable names behind platform-specific code, test for availability at build time where practical, and provide a fallback behavior.

Some options, including congestion-control selection or packet-filter attachment, can be restricted by system policy or capabilities such as CAP_NET_ADMIN. Treat a successful compile as insufficient evidence that a deployment may set the option.

Quick Recap

A practical checklist

  1. Confirm that the descriptor is the intended socket and that its address family and type match the option.
  2. Look up the option under the correct protocol manual and copy its required value type and length.
  3. Choose the lifecycle point: before bind(), before listen(), before connect(), or after connection establishment.
  4. Pass the object address and sizeof object; do not pass a pointer size accidentally.
  5. Check for -1, record errno, and distinguish an invalid argument from an unsupported option.
  6. Use getsockopt() when you need to verify a setting or inspect a read-only status such as SO_ACCEPTCONN.
  7. Test on every target operating system and kernel version when using Linux-specific options.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.