libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
SocketAddress Class Reference

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 NetworkAddressaddress () 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< SocketAddressfromString (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< SocketAddressfromSockAddr (const struct sockaddr *addr, size_t len)
 Constructs from a POSIX/Windows sockaddr structure.
 

Detailed Description

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.

Example
auto [addr, err] = SocketAddress::fromString("192.168.1.1:5004");
CHECK(addr.address().isIPv4());
CHECK(addr.port() == 5004);
SocketAddress mc(Ipv4Address(239, 0, 0, 1), 5004);
CHECK(mc.isMulticast());
IPv4 network address.
Definition ipv4address.h:51
Dynamic array container wrapping std::vector.
Definition list.h:40
Network address with port number.
Definition socketaddress.h:50
static Result< SocketAddress > fromString(const String &hostPort)
Parses a "host:port" string into a SocketAddress.

Constructor & Destructor Documentation

◆ SocketAddress() [1/3]

SocketAddress::SocketAddress ( const NetworkAddress address,
uint16_t  port 
)
inline

Constructs from a NetworkAddress and port.

Parameters
addressThe network address.
portThe port number.

◆ SocketAddress() [2/3]

SocketAddress::SocketAddress ( const Ipv4Address addr,
uint16_t  port 
)
inline

Constructs from an IPv4 address and port.

Parameters
addrThe IPv4 address.
portThe port number.

◆ SocketAddress() [3/3]

SocketAddress::SocketAddress ( const Ipv6Address addr,
uint16_t  port 
)
inline

Constructs from an IPv6 address and port.

Parameters
addrThe IPv6 address.
portThe port number.

Member Function Documentation

◆ any()

static SocketAddress SocketAddress::any ( uint16_t  port)
inlinestatic

Returns INADDR_ANY with the given port.

Parameters
portThe port number.
Returns
A SocketAddress bound to any interface on the given port.

◆ fromSockAddr()

static Result< SocketAddress > SocketAddress::fromSockAddr ( const struct sockaddr addr,
size_t  len 
)
static

Constructs from a POSIX/Windows sockaddr structure.

Parameters
addrPointer to a sockaddr (sockaddr_in or sockaddr_in6).
lenLength of the sockaddr structure.
Returns
A Result containing the address and Error::Ok, or Error::Invalid if the sockaddr is not recognized.

◆ fromString()

static Result< SocketAddress > SocketAddress::fromString ( const String hostPort)
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
Parameters
hostPortThe string to parse.
Returns
A Result containing the parsed address and Error::Ok, or a null address and Error::Invalid on parse failure.

◆ localhost()

static SocketAddress SocketAddress::localhost ( uint16_t  port)
inlinestatic

Returns localhost (127.0.0.1) with the given port.

Parameters
portThe port number.
Returns
A SocketAddress bound to localhost on the given port.

◆ toSockAddr()

size_t SocketAddress::toSockAddr ( struct sockaddr_storage storage) const

Fills a sockaddr_storage with this address and port.

Parameters
[out]storageThe structure to fill.
Returns
The size of the filled sockaddr structure, or 0 on failure.

◆ toString()

String SocketAddress::toString ( ) const

Returns a "host:port" string representation.

IPv6 addresses are enclosed in brackets: "[::1]:5004".

Returns
The address as a string.

The documentation for this class was generated from the following file: