![]() |
jsocketpp 1.0
A cross-platform C++20 socket library.
|
This document describes the various socket options supported by jsocketpp, how they relate to each socket type, and best practices for using them in your applications. It is intended as a practical reference for users and contributors.
Socket options allow you to change the behavior of a socket at runtime, such as buffer sizes, address reuse, and special TCP or UDP features. They are configured using the setsockopt() and getsockopt() system calls, but jsocketpp provides convenient, type-safe, and well-documented wrappers.
jsocketpp aims to abstract away the differences between Linux, macOS, and Windows. Most options are available on all major platforms, but some are platform-specific. Where relevant, this is noted below.
| Option | Description |
|---|---|
| setReuseAddress(bool) | Allow multiple sockets to bind to the same address/port. |
| getReuseAddress() const | Query if address reuse is enabled. |
On some Unix systems, you may also find SO_REUSEPORT for advanced load balancing, but it is not universally available.
| Option | Description |
|---|---|
| setNoDelay(bool) | Disable Nagle’s algorithm for lower latency (TCP_NODELAY). |
| getNoDelay() const | Query if Nagle’s algorithm is disabled. |
| setKeepAlive(bool) | Enable TCP keepalive packets (helps detect broken connections). |
| getKeepAlive() const | Query if keepalive is enabled. |
| setLinger(bool, int) | Control close() behavior: wait N seconds to send unsent data. |
| getLinger() const | Get linger status and timeout. |
| setSendBufferSize(int) | Set the size of the send buffer. |
| getSendBufferSize() const | Get the size of the send buffer. |
| setReceiveBufferSize(int) | Set the size of the receive buffer. |
| getReceiveBufferSize() const | Get the size of the receive buffer. |
| setTimeout(int) | Set socket timeouts (already in your API). |
| Option | Description |
|---|---|
| setBroadcast(bool) | Enable sending to broadcast addresses (SO_BROADCAST). |
| getBroadcast() const | Query if broadcast is enabled. |
| setSendBufferSize(int) | Set the send buffer size. |
| getSendBufferSize() const | Get the send buffer size. |
| setReceiveBufferSize(int) | Set the receive buffer size. |
| getReceiveBufferSize() const | Get the receive buffer size. |
| setReuseAddress(bool) | Allow reuse of local address/port (useful for multicast). |
| getReuseAddress() const | Query if address reuse is enabled. |
| Option | Description |
|---|---|
| setMulticastLoopback(bool) | Control whether outgoing multicast is looped back. |
| getMulticastLoopback() const | Query loopback status. |
| setMulticastTTL(int) | Set the multicast time-to-live (scope). |
| getMulticastTTL() const | Get the current multicast TTL. |
| setMulticastInterface(const std::string&) | Set outgoing multicast interface by name/address. |
| getMulticastInterface() const | Query the multicast interface. |
| Option | Description |
|---|---|
| setSendBufferSize(int) | Set the send buffer size. |
| getSendBufferSize() const | Get the send buffer size. |
| setReceiveBufferSize(int) | Set the receive buffer size. |
| getReceiveBufferSize() const | Get the receive buffer size. |
| setPassCred(bool) | Linux: pass credentials with messages (SO_PASSCRED). |
| getPassCred() const | Query if credential passing is enabled. |
| setReuseAddress(bool) | Rarely used, but available. |
| Option | Socket | ServerSocket | DatagramSocket | MulticastSocket | UnixSocket |
|---|---|---|---|---|---|
| setReuseAddress | ✓ | ✓ | ✓ | ✓ | ✓ |
| setNoDelay | ✓ | ||||
| setKeepAlive | ✓ | ✓ | |||
| setLinger | ✓ | ✓ | |||
| setBroadcast | ✓ | ✓ | |||
| setSendBufferSize | ✓ | ✓ | ✓ | ✓ | ✓ |
| setReceiveBufferSize | ✓ | ✓ | ✓ | ✓ | ✓ |
| setTimeout | ✓ | ✓ | ✓ | ✓ | ✓ |
| setMulticastLoopback | ✓ | ||||
| setMulticastTTL | ✓ | ||||
| setMulticastInterface | ✓ | ||||
| setPassCred | ✓ |
While jsocketpp provides generic setOption(int level, int optname, int value) and getOption(int level, int optname) methods for full flexibility, dedicated methods offer:
Questions, suggestions, or improvements? Please open an issue or contribute directly!