|
| 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 &&other) noexcept |
| Move constructor transfers ownership of socket resources.
|
UnixSocket & | operator= (UnixSocket &&other) noexcept |
| Move assignment operator transfers ownership of socket resources.
|
| ~UnixSocket () noexcept |
| 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.
|
void | setOption (int level, int optName, int value) const |
| Set a socket option on the Unix domain socket.
|
int | getOption (int level, int optName) const |
| Retrieve the value of a socket option for the Unix domain socket.
|
template<> |
std::string | read () |
| Template specialization to read a string from the Unix domain 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:151
size_t write(std::string_view data) const
Writes data to the socket.
Definition UnixSocket.cpp:136
int main()
Definition client.cpp:89
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:13
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.
bool UnixSocket::isPathInUse |
( |
std::string_view | path | ) |
|
|
static |
Checks whether a UNIX domain socket path is currently in use (i.e., a process is listening on it).
This function attempts to connect to the given UNIX socket path. If the connection succeeds, the path is in use. If the connection fails with ECONNREFUSED or ENOENT, the path is not in use (either no process is listening, or the file does not exist).
- Parameters
-
path | The UNIX socket file system path to check. |
- Returns
- true if a process is listening on the socket at the given path, false otherwise.
void UnixSocket::setNonBlocking |
( |
bool | nonBlocking | ) |
const |
Sets the socket to non-blocking or blocking mode.
This function configures the socket to operate in either non-blocking or blocking mode, depending on the value of the nonBlocking parameter. In non-blocking mode, socket operations will return immediately if they cannot be completed, rather than blocking the calling thread.
- Parameters
-
nonBlocking | If true, sets the socket to non-blocking mode; if false, sets it to blocking mode. |