Network address with port number. More...
#include <socketaddress.h>
Public Member Functions | |
| SocketAddress ()=default | |
| Default constructor. Creates a null address with port 0. | |
| SocketAddress (const NetworkAddress &address, uint16_t port) | |
| Constructs from a NetworkAddress and port. | |
| SocketAddress (const Ipv4Address &addr, uint16_t port) | |
| Constructs from an IPv4 address and port. | |
| SocketAddress (const Ipv6Address &addr, uint16_t port) | |
| Constructs from an IPv6 address and port. | |
| const NetworkAddress & | address () const |
| Returns the network address component. | |
| void | setAddress (const NetworkAddress &address) |
| Sets the network address component. | |
| uint16_t | port () const |
| Returns the port number. | |
| void | setPort (uint16_t port) |
| Sets the port number. | |
| bool | isNull () const |
| Returns true if no address is set and port is 0. | |
| bool | isIPv4 () const |
| Returns true if the address is IPv4. | |
| bool | isIPv6 () const |
| Returns true if the address is IPv6. | |
| bool | isLoopback () const |
| Returns true if the address is a loopback address. | |
| bool | isMulticast () const |
| Returns true if the address is a multicast address. | |
| String | toString () const |
| Returns a "host:port" string representation. | |
| size_t | toSockAddr (struct sockaddr_storage *storage) const |
| Fills a sockaddr_storage with this address and port. | |
| bool | operator== (const SocketAddress &other) const |
| Equality comparison. | |
| bool | operator!= (const SocketAddress &other) const |
| Inequality comparison. | |
Static Public Member Functions | |
| static Result< SocketAddress > | fromString (const String &hostPort) |
| Parses a "host:port" string into a SocketAddress. | |
| static SocketAddress | any (uint16_t port) |
| Returns INADDR_ANY with the given port. | |
| static SocketAddress | localhost (uint16_t port) |
| Returns localhost (127.0.0.1) with the given port. | |
| static Result< SocketAddress > | fromSockAddr (const struct sockaddr *addr, size_t len) |
| Constructs from a POSIX/Windows sockaddr structure. | |
Network address with port number.
SocketAddress combines a NetworkAddress (IPv4, IPv6, or hostname) with a 16-bit port number. This is the address type used by all socket classes for bind, connect, and send/receive operations.
This class is not thread-safe. Concurrent access to a single instance requires external synchronization.
|
inline |
Constructs from a NetworkAddress and port.
| address | The network address. |
| port | The port number. |
|
inline |
Constructs from an IPv4 address and port.
| addr | The IPv4 address. |
| port | The port number. |
|
inline |
Constructs from an IPv6 address and port.
| addr | The IPv6 address. |
| port | The port number. |
|
inlinestatic |
Returns INADDR_ANY with the given port.
| port | The port number. |
|
static |
Constructs from a POSIX/Windows sockaddr structure.
| addr | Pointer to a sockaddr (sockaddr_in or sockaddr_in6). |
| len | Length of the sockaddr structure. |
|
static |
Parses a "host:port" string into a SocketAddress.
Supports the following formats:
"192.168.1.1:5004" — IPv4 with port"[::1]:5004" — IPv6 with port (brackets required)"hostname:5004" — hostname with port| hostPort | The string to parse. |
|
inlinestatic |
Returns localhost (127.0.0.1) with the given port.
| port | The port number. |
| size_t SocketAddress::toSockAddr | ( | struct sockaddr_storage * | storage | ) | const |
Fills a sockaddr_storage with this address and port.
| [out] | storage | The structure to fill. |
| String SocketAddress::toString | ( | ) | const |
Returns a "host:port" string representation.
IPv6 addresses are enclosed in brackets: "[::1]:5004".