174 explicit ServerSocket(
Port port, std::string_view localAddress = {},
bool autoBindListen =
true,
175 bool reuseAddress =
true,
int soTimeoutMillis = -1,
bool dualStack =
true);
316 rhs._srvAddrInfo =
nullptr;
317 rhs._selectedAddrInfo =
nullptr;
318 rhs._isBound =
false;
319 rhs._isListening =
false;
370 rhs._srvAddrInfo =
nullptr;
371 rhs._selectedAddrInfo =
nullptr;
372 rhs._isBound =
false;
373 rhs._isListening =
false;
507 void listen(
int backlog = 128);
570 [[nodiscard]]
Socket accept(std::optional<std::size_t> recvBufferSize = std::nullopt,
571 std::optional<std::size_t> sendBufferSize = std::nullopt,
572 std::optional<std::size_t> internalBufferSize = std::nullopt)
const;
635 [[nodiscard]]
Socket accept(
int timeoutMillis, std::optional<std::size_t> recvBufferSize = std::nullopt,
636 std::optional<std::size_t> sendBufferSize = std::nullopt,
637 std::optional<std::size_t> internalBufferSize = std::nullopt)
const;
704 [[nodiscard]] std::optional<Socket>
tryAccept(std::optional<std::size_t> recvBufferSize = std::nullopt,
705 std::optional<std::size_t> sendBufferSize = std::nullopt,
706 std::optional<std::size_t> internalBufferSize = std::nullopt)
const;
768 [[nodiscard]] std::optional<Socket>
tryAccept(
int timeoutMillis,
769 std::optional<std::size_t> recvBufferSize = std::nullopt,
770 std::optional<std::size_t> sendBufferSize = std::nullopt,
771 std::optional<std::size_t> internalBufferSize = std::nullopt)
const;
841 std::optional<std::size_t> sendBufferSize = std::nullopt,
842 std::optional<std::size_t> internalBufferSize = std::nullopt)
const;
899 [[nodiscard]] std::optional<Socket>
901 std::optional<std::size_t> sendBufferSize = std::nullopt,
902 std::optional<std::size_t> internalBufferSize = std::nullopt)
const;
956 [[nodiscard]] std::future<Socket>
acceptAsync(std::optional<std::size_t> recvBufferSize = std::nullopt,
957 std::optional<std::size_t> sendBufferSize = std::nullopt,
958 std::optional<std::size_t> internalBufferSize = std::nullopt)
const;
1020 void acceptAsync(std::function<
void(std::optional<Socket>, std::exception_ptr)> callback,
1021 std::optional<std::size_t> recvBufferSize = std::nullopt,
1022 std::optional<std::size_t> sendBufferSize = std::nullopt,
1023 std::optional<std::size_t> internalBufferSize = std::nullopt)
const;
1124 void setOption(
int level,
int optName,
int value);
1152 [[nodiscard]]
int getOption(
int level,
int optName)
const;
1288 [[nodiscard]]
bool waitReady(std::optional<int> timeoutMillis = std::nullopt)
const;
1334#if defined(IPV6_V6ONLY)
1466#if defined(SO_REUSEPORT)
1645 [[nodiscard]] std::tuple<std::size_t, std::size_t, std::size_t>
1646 resolveBuffers(
const std::optional<std::size_t> recv,
const std::optional<std::size_t> send,
1647 const std::optional<std::size_t>
internal)
const
TCP client socket abstraction for jsocketpp.
bool _isListening
True if the server socket is listening.
Definition ServerSocket.hpp:1658
Port _port
Port number the server will listen on.
Definition ServerSocket.hpp:1656
int _soTimeoutMillis
Timeout for accept(); -1 = no timeout, 0 = poll, >0 = timeout in milliseconds.
Definition ServerSocket.hpp:1659
std::size_t _defaultReceiveBufferSize
Default buffer size used for accepted client sockets when no specific size is provided.
Definition ServerSocket.hpp:1660
addrinfo * _selectedAddrInfo
Selected address info for binding.
Definition ServerSocket.hpp:1655
std::size_t _defaultSendBufferSize
Default send buffer size for accepted client sockets.
Definition ServerSocket.hpp:1662
SOCKET _serverSocket
Underlying socket file descriptor.
Definition ServerSocket.hpp:1653
bool _isBound
True if the server socket is bound.
Definition ServerSocket.hpp:1657
addrinfo * _srvAddrInfo
Address info for binding (from getaddrinfo)
Definition ServerSocket.hpp:1654
std::size_t _defaultInternalBufferSize
Default internal buffer size for accepted client sockets, used by some read() methods.
Definition ServerSocket.hpp:1663
TCP client connection abstraction (Java-like interface).
Definition Socket.hpp:91
Common platform and utility includes for jsocketpp.
std::uint16_t Port
Type alias representing a TCP or UDP port number (1–65535).
Definition common.hpp:315
constexpr std::size_t DefaultBufferSize
Default internal buffer size (in bytes) for TCP socket read operations.
Definition common.hpp:352
void close()
Closes the server socket and releases its associated system resources.
Definition ServerSocket.cpp:260
std::tuple< std::size_t, std::size_t, std::size_t > resolveBuffers(const std::optional< std::size_t > recv, const std::optional< std::size_t > send, const std::optional< std::size_t > internal) const
Resolves effective receive and send buffer sizes from optional user inputs.
Definition ServerSocket.hpp:1646
void setOption(int level, int optName, int value)
Set a socket option for the listening server socket.
Definition ServerSocket.cpp:453
bool isBound() const noexcept
Check if the server socket is bound to a local address.
Definition ServerSocket.hpp:463
std::size_t getEffectiveSendBufferSize(std::optional< std::size_t > sendBufferSize) const
Get the effective send buffer size to use for socket write operations.
Definition ServerSocket.hpp:1598
bool isListening() const noexcept
Check if the server socket is currently listening for incoming connections.
Definition ServerSocket.hpp:521
void bind()
Binds the server socket to the configured port and network interface.
Definition ServerSocket.cpp:273
void setSoTimeout(const int timeoutMillis)
Set the logical timeout (in milliseconds) for accepting client connections.
Definition ServerSocket.hpp:1314
std::size_t getDefaultReceiveBufferSize() const noexcept
Get the current default receive buffer size for accepted client sockets.
Definition ServerSocket.hpp:1393
void setReuseAddress(bool enable)
Enable or disable address reuse for this server socket.
Definition ServerSocket.cpp:223
bool getNonBlocking() const
Check if the server socket is in non-blocking mode.
Definition ServerSocket.cpp:504
SOCKET getHandle() const
Get the underlying native socket handle/descriptor.
Definition ServerSocket.hpp:1542
void setIPv6Only(bool enable)
Enable or disable IPv6-only mode for this server socket.
Definition ServerSocket.cpp:608
void listen(int backlog=128)
Marks the socket as a passive (listening) socket, ready to accept incoming TCP connection requests.
Definition ServerSocket.cpp:297
bool isValid() const noexcept
Check whether the server socket is currently open and valid.
Definition ServerSocket.hpp:1077
std::string getLocalSocketAddress() const
Get the local socket address (IP and port) to which the server socket is bound.
Definition ServerSocket.cpp:194
bool waitReady(std::optional< int > timeoutMillis=std::nullopt) const
Wait for the server socket to become ready to accept an incoming connection.
Definition ServerSocket.cpp:523
void setReusePort(bool enable)
Enable or disable the SO_REUSEPORT socket option.
Definition ServerSocket.cpp:593
std::future< Socket > acceptAsync(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Asynchronously accept an incoming client connection, returning a future.
Definition ServerSocket.cpp:419
bool getReuseAddress() const
Query whether the address reuse option is enabled on this server socket.
Definition ServerSocket.cpp:229
Socket acceptBlocking(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Accept an incoming client connection, always blocking until a client connects (unless the socket is s...
Definition ServerSocket.cpp:369
void setNonBlocking(bool nonBlocking)
Set the server socket to non-blocking or blocking mode.
Definition ServerSocket.cpp:485
int getSoTimeout() const noexcept
Get the logical timeout (in milliseconds) for accept operations.
Definition ServerSocket.hpp:1332
bool getReusePort() const
Query whether SO_REUSEPORT is enabled for this socket.
Definition ServerSocket.cpp:599
std::size_t getDefaultSendBufferSize() const noexcept
Get the current default send buffer size for accepted client sockets.
Definition ServerSocket.hpp:1426
ServerSocket & operator=(const ServerSocket &rhs)=delete
Copy assignment operator (deleted).
Socket accept(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Accept an incoming client connection, respecting the configured socket timeout.
Definition ServerSocket.cpp:305
std::optional< Socket > tryAccept(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Attempt to accept an incoming client connection, returning immediately or after the configured timeou...
Definition ServerSocket.cpp:337
ServerSocket & operator=(ServerSocket &&rhs) noexcept
Move assignment operator for ServerSocket.
Definition ServerSocket.hpp:349
~ServerSocket() noexcept
Destructor that automatically closes the server socket and releases all associated resources.
Definition ServerSocket.cpp:244
std::string getInetAddress() const
Get the local IP address to which the server socket is bound.
Definition ServerSocket.cpp:156
void setDefaultInternalBufferSize(const std::size_t size)
Set the per-instance default internal buffer size used for buffered read operations.
Definition ServerSocket.hpp:1445
void cleanupAndThrow(int errorCode)
Cleans up server socket resources and throws a SocketException.
Definition ServerSocket.cpp:140
std::size_t getDefaultInternalBufferSize() const noexcept
Get the per-instance default internal buffer size used for buffered read operations.
Definition ServerSocket.hpp:1464
ServerSocket(const ServerSocket &rhs)=delete
Copy constructor (deleted).
ServerSocket(ServerSocket &&rhs) noexcept
Move constructor that transfers ownership of server socket resources.
Definition ServerSocket.hpp:310
ServerSocket(Port port, std::string_view localAddress={}, bool autoBindListen=true, bool reuseAddress=true, int soTimeoutMillis=-1, bool dualStack=true)
Constructs a ServerSocket for listening to incoming TCP connections with full configuration control.
Definition ServerSocket.cpp:8
Port getLocalPort() const
Retrieve the local port number to which the server socket is bound.
Definition ServerSocket.cpp:173
void setDefaultSendBufferSize(const std::size_t size)
Set the default send buffer size for accepted client sockets.
Definition ServerSocket.hpp:1411
std::optional< Socket > acceptNonBlocking(std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt) const
Attempt to accept a client connection in non-blocking fashion.
Definition ServerSocket.cpp:388
std::size_t getEffectiveReceiveBufferSize(const std::optional< std::size_t > recvBufferSize) const
Get the effective receive buffer size to use for socket read operations.
Definition ServerSocket.hpp:1577
bool isClosed() const noexcept
Check if the server socket has been closed.
Definition ServerSocket.hpp:1091
std::size_t getEffectiveInternalBufferSize(std::optional< std::size_t > internalBufferSize) const
Get the effective internal buffer size to use for buffered socket read operations.
Definition ServerSocket.hpp:1624
int getOption(int level, int optName) const
Retrieve the current value of a socket option for the listening server socket.
Definition ServerSocket.cpp:465
static int getSocketReuseOption()
Returns the correct socket option constant for address reuse, depending on the platform.
Definition ServerSocket.cpp:213
void setDefaultReceiveBufferSize(const std::size_t size)
Set the default receive buffer size for accepted client sockets.
Definition ServerSocket.hpp:1378
bool getIPv6Only() const
Query whether IPv6-only mode is enabled.
Definition ServerSocket.cpp:626
Implementation-only utilities and platform abstractions for jsocketpp.
Definition BufferView.hpp:49
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:13
constexpr SOCKET INVALID_SOCKET
Definition common.hpp:220
int SOCKET
Definition common.hpp:219