|
| | UnixSocket (std::string_view path, std::size_t bufferSize=512) |
| | Constructs a UnixSocket and connects or binds to the specified path.
|
| | UnixSocket (const UnixSocket &)=delete |
| | Copy constructor deleted to prevent copying.
|
| UnixSocket & | operator= (const UnixSocket &)=delete |
| | Copy assignment operator deleted to prevent copying.
|
| | UnixSocket (UnixSocket &&rhs) noexcept |
| | Move constructor transfers ownership of socket resources.
|
| UnixSocket & | operator= (UnixSocket &&rhs) noexcept |
| | Move assignment operator transfers ownership of socket resources.
|
| | ~UnixSocket () noexcept override |
| | Destructor.
|
| void | bind () |
| | Binds the socket.
|
| void | listen (int backlog=SOMAXCONN) const |
| | Marks the socket as a passive socket to accept incoming connections.
|
| UnixSocket | accept () const |
| | Accepts an incoming connection.
|
| void | connect () |
| | Connects the socket.
|
| size_t | write (std::string_view data) const |
| | Writes data to the socket.
|
| size_t | read (char *buffer, std::size_t len) const |
| | Reads data from the socket into a buffer.
|
| template<typename T> |
| T | read () |
| | Reads a trivially copyable type from the socket.
|
| void | close () |
| | Closes the socket.
|
| bool | isValid () const |
| | Checks if the socket is valid (open).
|
| std::string | getSocketPath () const |
| | Returns the path of the Unix domain socket.
|
| void | setNonBlocking (bool nonBlocking) const |
| | Sets the socket to non-blocking or blocking mode.
|
| void | setTimeout (int millis) const |
| | Sets a timeout for socket operations.
|
| template<> |
| std::string | read () |
| | Template specialization to read a string from the Unix domain socket.
|
|
| | SocketOptions ()=delete |
| | Default constructor (deleted) for SocketOptions base class.
|
| | SocketOptions (const SOCKET sock) noexcept |
| | Initializes the socket option interface with a specific socket descriptor.
|
| virtual | ~SocketOptions ()=default |
| | Virtual destructor for safe polymorphic destruction.
|
| | SocketOptions (const SocketOptions &)=delete |
| | Copy constructor (deleted) for SocketOptions.
|
| | SocketOptions (SocketOptions &&) noexcept=default |
| | Move constructor for SocketOptions.
|
| SocketOptions & | operator= (const SocketOptions &)=delete |
| | Copy assignment operator (deleted) for SocketOptions.
|
| SocketOptions & | operator= (SocketOptions &&) noexcept=default |
| | Move assignment operator for SocketOptions.
|
| SOCKET | getSocketFd () const noexcept |
| | Retrieves the native socket handle (file descriptor or OS-level handle).
|
| void | setOption (int level, int optName, int value) |
| | Sets a low-level socket option on the underlying socket.
|
| void | setOption (int level, int optName, const void *value, socklen_t len) |
| | Sets a low-level socket option using a structured or binary value.
|
| int | getOption (int level, int optName) const |
| | Retrieves the current value of a low-level socket option.
|
| void | getOption (int level, int optName, void *result, socklen_t *len) const |
| | Retrieves a socket option into a structured or binary buffer.
|
| void | setReuseAddress (bool on) |
| | Enables or disables the SO_REUSEADDR socket option.
|
| bool | getReuseAddress () const |
| | Queries whether the socket is currently configured to allow address reuse.
|
| void | setReceiveBufferSize (std::size_t size) |
| | Sets the socket's receive buffer size (SO_RCVBUF).
|
| int | getReceiveBufferSize () const |
| | Retrieves the current receive buffer size (SO_RCVBUF) of the socket.
|
| void | setSendBufferSize (std::size_t size) |
| | Sets the socket's send buffer size (SO_SNDBUF).
|
| int | getSendBufferSize () const |
| | Retrieves the current send buffer size (SO_SNDBUF) of the socket.
|
| void | setSoLinger (bool enable, int seconds) |
| | Configures the socket's linger behavior (SO_LINGER) during close.
|
| std::pair< bool, int > | getSoLinger () const |
| | Retrieves the current SO_LINGER configuration of the socket.
|
| void | setKeepAlive (bool on) |
| | Enables or disables TCP-level keepalive behavior on the socket (SO_KEEPALIVE).
|
| bool | getKeepAlive () const |
| | Checks whether TCP-level keepalive (SO_KEEPALIVE) is currently enabled.
|
| void | setSoRecvTimeout (int millis) |
| | Sets the socket receive timeout (SO_RCVTIMEO) in milliseconds.
|
| void | setSoSendTimeout (int millis) |
| | Sets the socket send timeout (SO_SNDTIMEO) in milliseconds.
|
| int | getSoRecvTimeout () const |
| | Retrieves the socket receive timeout (SO_RCVTIMEO) in milliseconds.
|
| int | getSoSendTimeout () const |
| | Retrieves the socket send timeout (SO_SNDTIMEO) in milliseconds.
|
| void | setNonBlocking (bool nonBlocking) |
| | Enables or disables non-blocking mode on the socket.
|
| bool | getNonBlocking () const |
| | Queries whether the socket is currently in non-blocking mode.
|
| void | setTcpNoDelay (bool on) |
| | Enables or disables Nagle’s algorithm (TCP_NODELAY) on TCP sockets.
|
| bool | getTcpNoDelay () const |
| | Queries whether Nagle's algorithm (TCP_NODELAY) is currently disabled.
|
| void | setBroadcast (bool on) |
| | Enables or disables the ability to send UDP broadcast messages.
|
| bool | getBroadcast () const |
| | Checks whether the socket is currently allowed to send broadcast messages.
|
| void | setIPv6Only (bool enable) |
| | Enables or disables IPV6_V6ONLY mode for IPv6-capable sockets.
|
| bool | getIPv6Only () const |
| | Queries whether the IPV6_V6ONLY option is enabled on this socket.
|
| void | setReusePort (const bool enable) |
| | Enables or disables the SO_REUSEPORT socket option.
|
| bool | getReusePort () const |
| | Checks whether the SO_REUSEPORT option is currently enabled on the socket.
|
| void | setMulticastTTL (int ttl) |
| | Set the default multicast hop limit / TTL for this socket.
|
| int | getMulticastTTL () const |
| | Get the socket’s default multicast hop limit / TTL.
|
| void | setMulticastLoopback (bool enable) |
| | Enable or disable multicast loopback for this socket.
|
| bool | getMulticastLoopback () const |
| | Read the socket’s multicast loopback flag.
|
| void | setMulticastInterfaceIPv4 (in_addr addr) |
| | Select the outgoing IPv4 interface for multicast.
|
| void | setMulticastInterfaceIPv6 (unsigned int ifindex) |
| | Select the outgoing IPv6 interface for multicast.
|
| void | joinGroupIPv4 (in_addr group, in_addr iface) |
| | Join an IPv4 any-source multicast (ASM) group on a specific interface.
|
| void | leaveGroupIPv4 (in_addr group, in_addr iface) |
| | Leave a previously joined IPv4 multicast group on a specific interface.
|
| void | joinGroupIPv6 (in6_addr group, unsigned int ifindex) |
| | Join an IPv6 any-source multicast (ASM) group on a specific interface index.
|
| void | leaveGroupIPv6 (in6_addr group, unsigned int ifindex) |
| | Leave a previously joined IPv6 multicast group on a specific interface index.
|
| void | setSocketFd (const SOCKET sock) noexcept |
| | Updates the socket descriptor used by this object.
|
| virtual bool | isPassiveSocket () const noexcept |
| | Indicates whether the socket behaves as a passive (listening) socket.
|
| static bool | is_ipv4_multicast (const in_addr v4) |
| | Test whether an IPv4 address is in the multicast range (224.0.0.0/4).
|
| static bool | is_ipv6_multicast (const in6_addr &v6) |
| | Test whether an IPv6 address is in the multicast range (ff00::/8).
|
| static int | detectFamily (SOCKET fd) |
| | Determine the address family (AF_INET or AF_INET6) of a socket.
|
Cross-platform abstraction for Unix domain sockets.
The UnixSocket class provides a convenient, modern C++20-style interface for using Unix domain sockets, also known as AF_UNIX sockets. Unix sockets are used for fast inter-process communication (IPC) on the same host, and use a file path on the local file system for addressing.
On POSIX platforms (Linux, macOS), AF_UNIX sockets are widely supported. On Windows, support is available in Windows 10 (1803+, build 17134) and newer.
Main Features
- Bind: Create and bind a socket to a filesystem path for listening/accepting connections.
- Listen/Accept: Wait for incoming connections and accept them (server-side).
- Connect: Connect to a Unix domain socket path (client-side).
- Read/Write: Send and receive data over the connection, supporting both binary and string types.
- Non-blocking & Timeout: Support for non-blocking I/O and operation timeouts.
- Automatic cleanup: Unlinks the socket file on destruction.
- Note
- Not thread-safe. Each UnixSocket should only be used from one thread at a time.
-
On Windows, AF_UNIX is supported on Windows 10 1803 and later only. On unsupported platforms, attempting to use this class will result in a compilation error.
Example: Simple Echo Server
server.bind();
server.listen();
while (true) {
std::string data = client.
read<std::string>();
}
}
Unix domain socket abstraction for jsocketpp.
UnixSocket(std::string_view path, std::size_t bufferSize=512)
Constructs a UnixSocket and connects or binds to the specified path.
Definition UnixSocket.cpp:8
size_t read(char *buffer, std::size_t len) const
Reads data from the socket into a buffer.
Definition UnixSocket.cpp:163
size_t write(std::string_view data) const
Writes data to the socket.
Definition UnixSocket.cpp:145
int main()
Definition client.cpp:89
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:16
Example: Simple Client
sock.connect();
sock.write("Hello, Unix domain socket!");
std::string response = sock.read<std::string>();
}
Limitations
- Not suitable for remote (network) connections; only for IPC on the same host.
- The socket file is automatically deleted on destruction.