Socket class
Owns one socket descriptor and closes it on destruction.
On this page
- Constructors
- Socket
- ~Socket
- Methods
- Startup
- Cleanup
- operator=
- IsValid
- Create
- Attach
- Close
- Bind
- Connect
- GetPeerName
- GetSockName
- GetOption
- SetOption
- Shutdown
- Listen
- GetOptionIsListening
- SetOptionBroadcast
- GetOptionBroadcast
- SetOptionDontRoute
- GetOptionDontRoute
- GetOptionError
- SetOptionKeepAlive
- GetOptionKeepAlive
- SetOptionLinger
- GetOptionLinger
- SetOptionOobInLine
- GetOptionOobInLine
- SetOptionReceiveBufferSize
- GetOptionReceiveBufferSize
- SetOptionReuseAddress
- GetOptionReuseAddress
- SetOptionSendBufferSize
- GetOptionSendBufferSize
- GetOptionType
- GetSocket
- Use
import ThinkMeta.ConcurrentTasks.Posix.Socket;- Namespace
ThinkMeta::ConcurrentTasks::Posix- Platforms
- Linux
Syntax
class SocketRemarks
Holds -1 when there is no socket. All members report failures as an errno (ErrorCode, Error::Success on success). Members that use the socket require one to be held (asserted in Debug builds). Plain blocking calls: called from a task they block the scheduler thread. No member is synchronized.
See also
Windows::Socket, AsyncIo::AsyncSocket
Constructors
Socket
~Socket
Methods
Startup
Cleanup
operator=
IsValid
true if the object holds a socket.
Syntax
bool IsValid() const noexcept;Return value
Type: bool
true if the object holds a socket; false if it has none.
Create
Creates the socket (socket, with SOCK_CLOEXEC).
Syntax
ErrorCode Create(int addressFamily, int type, int protocol) noexcept;Parameters
addressFamilyint- The address family, e.g.
AF_INETorAF_INET6. typeint- The socket type, e.g.
SOCK_STREAMorSOCK_DGRAM. protocolint- The protocol, e.g.
IPPROTO_TCP;0lets the system choose.
Return value
Type: ErrorCode
Error::Success, or the errno of socket.
Remarks
The object must not hold a socket yet.
See also
Attach
Takes ownership of a descriptor, e.g. one an accept returned.
Syntax
void Attach(int socket) noexcept;Parameters
socketint- The descriptor; this object closes it from now on.
Remarks
The object must not hold a socket yet.
Close
Closes the socket (close), if there is one; the object is then without socket.
Syntax
ErrorCode Close() noexcept;Return value
Type: ErrorCode
Error::Success, or the errno of close.
Remarks
The descriptor is given up also when close reports an error, as Linux has released it then too. Operations in flight must have completed before (see AsyncIo::AsyncSocket).
See also
Bind
Binds the socket to addr (bind).
Syntax
template <SockAddrType _SockAddr> ErrorCode Bind(const _SockAddr& addr) noexcept;Template parameters
_SockAddrSockAddrType- The address structure type; its size is passed as the address length.
Parameters
addrconst _SockAddr&- The local address and port; port
0lets the system choose one.
Return value
Type: ErrorCode
Error::Success, or the errno of bind.
Connect
Connects to addr (connect); blocks the calling thread.
Syntax
template <SockAddrType _SockAddr> ErrorCode Connect(const _SockAddr& addr) noexcept;Template parameters
_SockAddrSockAddrType- The address structure type; its size is passed as the address length.
Parameters
addrconst _SockAddr&- The remote address and port.
Return value
Type: ErrorCode
Error::Success, or the errno of connect.
GetPeerName
Gets the peer's address (getpeername).
Syntax
template <SockAddrType _SockAddr> ErrorCode GetPeerName(_SockAddr& addr) noexcept;Template parameters
_SockAddrSockAddrType- The address structure type; its size is passed as the buffer length.
Parameters
addr_SockAddr&- Receives the peer's address; must be large enough for the socket's address family.
Return value
Type: ErrorCode
Error::Success, or the errno of getpeername.
See also
GetSockName
Gets the local address (getsockname), e.g. the port chosen by Bind with port 0.
Syntax
template <SockAddrType _SockAddr> ErrorCode GetSockName(_SockAddr& addr) noexcept;Template parameters
_SockAddrSockAddrType- The address structure type; its size is passed as the buffer length.
Parameters
addr_SockAddr&- Receives the local address; must be large enough for the socket's address family.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockname.
See also
GetOption
Gets a socket option into a raw buffer (getsockopt).
Syntax
ErrorCode GetOption(
int level,
int optionName,
void* optionValue,
socklen_t* valueLength
) noexcept;Parameters
levelint- The option level, e.g.
SOL_SOCKET,IPPROTO_IP,IPPROTO_IPV6,IPPROTO_TCP. optionNameint- The option at that level, e.g.
SO_RCVBUF. optionValuevoid*- Required. Receives the option value.
valueLengthsocklen_t*- Required. In: the size of
optionValuein bytes; out: the size written.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
See also
Gets a socket option of type T (buffer size sizeof(T)).
Syntax
template <typename T>
ErrorCode GetOption(int level, int optionName, T* optionValue) noexcept;Template parameters
Ttypename- The option's value type, e.g.
intorlinger.
Parameters
levelint- The option level, e.g.
SOL_SOCKET,IPPROTO_IP,IPPROTO_IPV6,IPPROTO_TCP. optionNameint- The option at that level, e.g.
SO_RCVBUF. optionValueT*- Required. Receives the option value.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
Gets an int-sized on/off socket option as bool.
Syntax
template <>
ErrorCode GetOption<bool>(int level, int optionName, bool* optionValue) noexcept;Parameters
levelint- The option level, e.g.
SOL_SOCKET. optionNameint- The option at that level, e.g.
SO_KEEPALIVE. optionValuebool*- Required. Receives
trueif the option is set (non-zero); unchanged on failure.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
SetOption
Sets a socket option from a raw buffer (setsockopt).
Syntax
ErrorCode SetOption(
int level,
int optionName,
const void* optionValue,
socklen_t valueLength
) noexcept;Parameters
levelint- The option level, e.g.
SOL_SOCKET,IPPROTO_IP,IPPROTO_IPV6,IPPROTO_TCP. optionNameint- The option at that level, e.g.
SO_RCVBUF. optionValueconst void*- Required. The new option value.
valueLengthsocklen_t- The size of
optionValuein bytes.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
See also
Sets a socket option from a value of type T.
Syntax
template <typename T>
ErrorCode SetOption(int level, int optionName, const T& optionValue) noexcept;Template parameters
Ttypename- The option's value type, e.g.
intorlinger.
Parameters
levelint- The option level, e.g.
SOL_SOCKET,IPPROTO_IP,IPPROTO_IPV6,IPPROTO_TCP. optionNameint- The option at that level, e.g.
SO_RCVBUF. optionValueconst T&- The new option value.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
Remarks
The value is passed by address with size sizeof(T).
Sets an int-sized on/off socket option from bool.
Syntax
template <>
ErrorCode SetOption<bool>(
int level,
int optionName,
const bool& optionValue
) noexcept;Parameters
levelint- The option level, e.g.
SOL_SOCKET. optionNameint- The option at that level, e.g.
SO_KEEPALIVE. optionValueconst bool&trueto enable the option,falseto disable it.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
Shutdown
Disables sends and/or receives (shutdown).
Syntax
ErrorCode Shutdown(int how) noexcept;Parameters
howintSHUT_RD,SHUT_WRorSHUT_RDWR(the values ofSD_RECEIVE,SD_SEND,SD_BOTH).
Return value
Type: ErrorCode
Error::Success, or the errno of shutdown.
Listen
Puts the bound socket into listening state (listen).
Syntax
ErrorCode Listen(int backlog = SOMAXCONN) noexcept;Parameters
backlogint- The maximum length of the queue of pending connections;
SOMAXCONNfor the system's limit.Default:SOMAXCONN
Return value
Type: ErrorCode
Error::Success, or the errno of listen.
GetOptionIsListening
Gets whether the socket listens (SO_ACCEPTCONN).
Syntax
ErrorCode GetOptionIsListening(bool* isListening) noexcept;Parameters
isListeningbool*- Required. Receives
trueif the socket is listening.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
SetOptionBroadcast
Allows sending broadcasts (SO_BROADCAST).
Syntax
ErrorCode SetOptionBroadcast(bool enable) noexcept;Parameters
enablebooltrueto allow broadcasts.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
GetOptionBroadcast
Gets whether broadcasts are allowed (SO_BROADCAST).
Syntax
ErrorCode GetOptionBroadcast(bool* enable) noexcept;Parameters
enablebool*- Required. Receives the setting.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
SetOptionDontRoute
Sends without routing, to directly connected hosts only (SO_DONTROUTE).
Syntax
ErrorCode SetOptionDontRoute(bool dontRoute) noexcept;Parameters
dontRoutebooltrueto bypass routing.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
GetOptionDontRoute
Gets whether routing is bypassed (SO_DONTROUTE).
Syntax
ErrorCode GetOptionDontRoute(bool* dontRoute) noexcept;Parameters
dontRoutebool*- Required. Receives the setting.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
GetOptionError
Gets and clears the socket's pending error (SO_ERROR).
Syntax
Parameters
errorErrorCode*- Required. Receives the pending
errno;0if none.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
SetOptionKeepAlive
Sends keep-alive probes on an idle connection (SO_KEEPALIVE).
Syntax
ErrorCode SetOptionKeepAlive(bool keepAlive) noexcept;Parameters
keepAlivebooltrueto send keep-alives.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
GetOptionKeepAlive
Gets whether keep-alives are sent (SO_KEEPALIVE).
Syntax
ErrorCode GetOptionKeepAlive(bool* keepAlive) noexcept;Parameters
keepAlivebool*- Required. Receives the setting.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
SetOptionLinger
Sets how closing treats unsent data (SO_LINGER).
Syntax
ErrorCode SetOptionLinger(const linger& l) noexcept;Parameters
lconst linger&- On/off and the time in seconds to wait for unsent data.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
GetOptionLinger
Gets how closing treats unsent data (SO_LINGER).
Syntax
ErrorCode GetOptionLinger(linger* l) noexcept;Parameters
llinger*- Required. Receives the setting.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
SetOptionOobInLine
Receives out-of-band data in the normal stream (SO_OOBINLINE).
Syntax
ErrorCode SetOptionOobInLine(bool inLine) noexcept;Parameters
inLinebooltrueto receive it in line.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
GetOptionOobInLine
Gets whether out-of-band data comes in line (SO_OOBINLINE).
Syntax
ErrorCode GetOptionOobInLine(bool* inLine) noexcept;Parameters
inLinebool*- Required. Receives the setting.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
SetOptionReceiveBufferSize
Sets the receive buffer size (SO_RCVBUF).
Syntax
ErrorCode SetOptionReceiveBufferSize(std::uint32_t size) noexcept;Parameters
sizestd::uint32_t- The size in bytes; Linux doubles it for its bookkeeping.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
GetOptionReceiveBufferSize
Gets the receive buffer size (SO_RCVBUF).
Syntax
ErrorCode GetOptionReceiveBufferSize(std::uint32_t* size) noexcept;Parameters
sizestd::uint32_t*- Required. Receives the size in bytes.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
SetOptionReuseAddress
Allows binding to an address in use (SO_REUSEADDR).
Syntax
ErrorCode SetOptionReuseAddress(bool reuseAddress) noexcept;Parameters
reuseAddressbooltrueto allow it.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
GetOptionReuseAddress
Gets the reuse-address setting (SO_REUSEADDR).
Syntax
ErrorCode GetOptionReuseAddress(bool* reuseAddress) noexcept;Parameters
reuseAddressbool*- Required. Receives the setting.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
SetOptionSendBufferSize
Sets the send buffer size (SO_SNDBUF).
Syntax
ErrorCode SetOptionSendBufferSize(std::uint32_t size) noexcept;Parameters
sizestd::uint32_t- The size in bytes; Linux doubles it for its bookkeeping.
Return value
Type: ErrorCode
Error::Success, or the errno of setsockopt.
GetOptionSendBufferSize
Gets the send buffer size (SO_SNDBUF).
Syntax
ErrorCode GetOptionSendBufferSize(std::uint32_t* size) noexcept;Parameters
sizestd::uint32_t*- Required. Receives the size in bytes.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
GetOptionType
Gets the socket type (SO_TYPE), e.g. SOCK_STREAM.
Syntax
ErrorCode GetOptionType(int* type) noexcept;Parameters
typeint*- Required. Receives the type.
Return value
Type: ErrorCode
Error::Success, or the errno of getsockopt.
GetSocketprotected
The descriptor, still owned by this object.
Syntax
int GetSocket() const noexcept;Return value
Type: int
The descriptor, or -1 if none is held; do not close it.
Remarks
Protected, as on Windows: the classes built on the socket use it.
Generated from ThinkMeta.ConcurrentTasks.API @ da816bc (2026-11-06).