Structure
SocketAddress
A network socket address backed by sockaddr_storage (IPv4 or IPv6).
struct SocketAddress
Mentioned In
Overview
SocketAddress is a value-type Swift wrapper over POSIX sockaddr_storage โ the address-family-agnostic container defined in RFC 2553 ยง3.10 and the POSIX sys/socket.h specification. It is large enough to hold any supported address family (sockaddr_in for IPv4, sockaddr_in6 for IPv6) without heap allocation, and is safe to pass across task boundaries thanks to its Sendable conformance (all stored properties are value types).
Byte order
The port property and the port accessor transparently handle the network-to-host byte-order conversion: input to ipv4(_:port:) / ipv6(_:port:) / anyIPv4(port:) is in host byte order, stored internally in network byte order (big-endian), and returned in host byte order. Callers never need to touch htons / ntohs.
Address parsing
Parsing is delegated to inet_pton(3), which accepts standard numeric notation ("127.0.0.1", "::1", "2001:db8::1"). Hostname resolution via DNS is not performed โ pass a literal IP address. For DNS, perform the lookup separately (e.g. via Foundation / Network.framework) and feed the resulting address string into one of the factory methods.
Usage
import Event
let loopback4 = try SocketAddress.ipv4("127.0.0.1", port: 8080)
let loopback6 = try SocketAddress.ipv6("::1", port: 8080)
let wildcard = SocketAddress.anyIPv4(port: 8080)
print(loopback4.port) // 8080
See Also
connect(to:loop:), listen(on:backlog:loop:), SocketError.invalidAddress(_:).
Topics
Instance Properties
Type Methods