<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "Event",
  "identifier" : "/documentation/Event/SocketAddress",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Event"
    ],
    "preciseIdentifier" : "s:5Event13SocketAddressV"
  },
  "title" : "SocketAddress"
}
-->

# SocketAddress

A network socket address backed by `sockaddr_storage` (IPv4 or IPv6).

```
struct SocketAddress
```

## Overview

`SocketAddress` is a value-type Swift wrapper over POSIX `sockaddr_storage` — the
address-family-agnostic container defined in [RFC 2553 §3.10](https://datatracker.ietf.org/doc/html/rfc2553#section-3.10) and the
[POSIX sys/socket.h specification](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html). 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`](/documentation/Event/SocketAddress/port) accessor transparently handle the network-to-host
byte-order conversion: input to [`ipv4(_:port:)`](/documentation/Event/SocketAddress/ipv4(_:port:)) / [`ipv6(_:port:)`](/documentation/Event/SocketAddress/ipv6(_:port:)) / [`anyIPv4(port:)`](/documentation/Event/SocketAddress/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

```swift
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
```

> SeeAlso: ``doc://Event/documentation/Event/Socket/connect(to:loop:)``, ``doc://Event/documentation/Event/Socket/listen(on:backlog:loop:)``,
> ``doc://Event/documentation/Event/SocketError/invalidAddress(_:)``.