# Poco::Net

# class SocketAddress

Library: Net
Package: NetCore
Header: Poco/Net/SocketAddress.h

## Description

This class represents an internet (IP) endpoint/socket address. The address can belong either to the IPv4, IPv6 or Unix local family. IP addresses consist of a host address and a port number. An Unix local socket address consists of a path to socket file. Abstract local sockets, which operate without the need for interaction with the filesystem, are supported on Linux only.

## Member Summary

Member Functions: addr, af, family, host, init, isUnixLocal, length, operator !=, operator <, operator =, operator ==, port, resolveService, toString

## Types Aliases

### Family

using Family = AddressFamily::Family;

## Enumerations

### Anonymous

MAX_ADDRESS_LENGTH = sizeof (struct sockaddr_un)

Maximum length in bytes of a socket address.

## Constructors

### SocketAddress

SocketAddress();

Creates a wildcard (all zero) IPv4 SocketAddress.

### SocketAddress

explicit SocketAddress(
Family family
);

Creates a SocketAddress with unspecified (wildcard) IP address of the given family.

### SocketAddress

explicit SocketAddress(
Poco::UInt16 port
);

Creates a SocketAddress with unspecified (wildcard) IP address and given port number.

### SocketAddress

explicit SocketAddress(
const std::string & hostAndPort
);

Creates a SocketAddress from an IP address or host name and the port number/service name. Host name/address and port number must be separated by a colon. In case of an IPv6 address, the address part must be enclosed in brackets.

Examples:

    192.168.1.10:80
    [::ffff:192.168.1.120]:2040
    www.appinf.com:8080

On platforms supporting UNIX_LOCAL sockets, hostAndPort can also be a valid absolute local socket file path.

Examples:

    /tmp/local.sock
    C:\Temp\local.sock

On Linux, abstract local sockets are supported as well. Abstract local sockets operate in a namespace that has no need for a filesystem. They are identified by a null byte at the beginning of the path.

Example:

    \0abstract.sock

### SocketAddress

SocketAddress(
const SocketAddress & addr
);

Creates a SocketAddress by copying another one.

### SocketAddress

SocketAddress(
SocketAddress && addr
);

Creates a SocketAddress by moving another one.

### SocketAddress

SocketAddress(
const IPAddress & hostAddress,
Poco::UInt16 portNumber
);

Creates a SocketAddress from an IP address and given port number.

### SocketAddress

SocketAddress(
Family family,
Poco::UInt16 port
);

Creates a SocketAddress with unspecified (wildcard) IP address of the given family, and given port number.

### SocketAddress

SocketAddress(
const std::string & hostAddress,
Poco::UInt16 portNumber
);

Creates a SocketAddress from an IP address and given port number.

The IP address must either be a domain name, or it must be in dotted decimal (IPv4) or hex string (IPv6) format.

### SocketAddress

SocketAddress(
const std::string & hostAddress,
const std::string & portNumber
);

Creates a SocketAddress from an IP address and the service name or port number.

The IP address must either be a domain name, or it must be in dotted decimal (IPv4) or hex string (IPv6) format.

The given port must either be a decimal port number, or a service name.

### SocketAddress

SocketAddress(
Family family,
const std::string & addr
);

Creates a SocketAddress of the given family from a string representation of the address, which is either (1) an IP address and port number, separated by a colon for IPv4 or IPv6 addresses, or (2) path for UNIX_LOCAL sockets. See `SocketAddress(const string&)` documentation for more details.

### SocketAddress

SocketAddress(
const struct sockaddr * addr,
socklen_t length
);

Creates a SocketAddress from a native socket address.

### SocketAddress

SocketAddress(
Family family,
const std::string & hostAddress,
Poco::UInt16 portNumber
);

Creates a SocketAddress from an IP address and given port number.

The IP address must either be a domain name, or it must be in dotted decimal (IPv4) or hex string (IPv6) format.

If a domain name is given in hostAddress, it is resolved and the address matching the given family is used. If no address matching the given family is found, or the IP address given in hostAddress does not match the given family, an AddressFamilyMismatchException is thrown.

### SocketAddress

SocketAddress(
Family family,
const std::string & hostAddress,
const std::string & portNumber
);

Creates a SocketAddress from an IP address and the service name or port number.

The IP address must either be a domain name, or it must be in dotted decimal (IPv4) or hex string (IPv6) format.

The given port must either be a decimal port number, or a service name.

If a domain name is given in hostAddress, it is resolved and the address matching the given family is used. If no address matching the given family is found, or the IP address given in hostAddress does not match the given family, an AddressFamilyMismatchException is thrown.

## Destructor

### ~SocketAddress

~SocketAddress();

Destroys the SocketAddress.

## Member Functions

### addr

const struct sockaddr * addr() const;

Returns a pointer to the internal native socket address.

### af

int af() const;

Returns the address family (AF_INET or AF_INET6) of the address.

### family

Family family() const;

Returns the address family of the host's address.

### host

IPAddress host() const;

Returns the host IP address.

### isUnixLocal

static bool isUnixLocal(
const std::string & hostAndPort
);

Returns true if and only if `hostAndPort` is an absolute file path.

### length

socklen_t length() const;

Returns the length of the internal native socket address.

### operator !=

bool operator != (
const SocketAddress & socketAddress
) const;

### operator <

bool operator < (
const SocketAddress & socketAddress
) const;

### operator =

SocketAddress & operator = (
const SocketAddress & socketAddress
);

Assigns another SocketAddress.

### operator =

SocketAddress & operator = (
SocketAddress && socketAddress
);

Move-assigns another SocketAddress.

### operator ==

bool operator == (
const SocketAddress & socketAddress
) const;

### port

Poco::UInt16 port() const;

Returns the port number.

### toString

std::string toString() const;

Returns a string representation of the address.

### init

void init(
const IPAddress & hostAddress,
Poco::UInt16 portNumber
);

### init

void init(
const std::string & hostAddress,
Poco::UInt16 portNumber
);

### init

void init(
Family family,
const std::string & hostAddress,
Poco::UInt16 portNumber
);

### init

void init(
Family family,
const std::string & address
);

### init

void init(
const std::string & hostAndPort
);

### resolveService

Poco::UInt16 resolveService(
const std::string & service
);

## Variables

### IPv4

static const Family IPv4 = AddressFamily::IPv4;

### IPv6

static const Family IPv6 = AddressFamily::IPv6;

### UNIX_LOCAL

static const Family UNIX_LOCAL = AddressFamily::UNIX_LOCAL;