|
8 | 8 | */
|
9 | 9 |
|
10 | 10 | // @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
|
11 |
| -/// An opaque type holding a socket address and port number in some address family. |
| 11 | +/// An opaque type representing a socket address in some address family, |
| 12 | +/// such as an IP address along with a port number. |
12 | 13 | ///
|
13 |
| -/// TODO: Show examples of creating an ipv4 and ipv6 address |
| 14 | +/// `SocketAddress` values can be passed directly to `SocketDescriptor.connect` |
| 15 | +/// or `.bind` to establish a network connection. |
14 | 16 | ///
|
15 |
| -/// The corresponding C type is `sockaddr_t` |
| 17 | +/// We can use the `SocketAddress.resolveName` method resolve a pair of |
| 18 | +/// host/service name strings to a list of socket addresses: |
| 19 | +/// |
| 20 | +/// let results = |
| 21 | +/// try SocketAddress.resolveName(hostname: "swift.org", service: "https") |
| 22 | +/// for result in results { |
| 23 | +/// let try socket = |
| 24 | +/// SocketDescriptor.open(result.domain, result.type, result.protocol) |
| 25 | +/// do { |
| 26 | +/// try socket.connect(to: result.address) |
| 27 | +/// } catch { |
| 28 | +/// try? socket.close() |
| 29 | +/// throw error |
| 30 | +/// } |
| 31 | +/// return socket |
| 32 | +/// } |
| 33 | +/// |
| 34 | +/// To create an IPv4, IPv6 or Local domain address, we can use convenience |
| 35 | +/// initializers that take the corresponding information: |
| 36 | +/// |
| 37 | +/// let ipv4 = SocketAddress(ipv4: "127.0.0.1", port: 8080)! |
| 38 | +/// let ipv6 = SocketAddress(ipv6: "::1", port: 80)! |
| 39 | +/// let local = SocketAddress(local: "/var/run/example.sock") |
| 40 | +/// |
| 41 | +/// (Note that you may prefer to use the concrete address types |
| 42 | +/// `SocketAddress.IPv4`, `SocketAddress.IPv6` and `SocketAddress.Local` |
| 43 | +/// instead -- they provide easy access to the address parameters.) |
| 44 | +/// |
| 45 | +/// `SocketAddress` also provides ways to access its underlying contents |
| 46 | +/// as a raw unsafe memory buffer. This is useful for dealing with address |
| 47 | +/// families that `System` doesn't model, or for passing the socket address |
| 48 | +/// to C functions that expect a pointer to a `sockaddr` value. |
| 49 | +/// |
| 50 | +/// `SocketAddress` stores its contents in a managed storage buffer, and |
| 51 | +/// it can serve as a reusable receptacle for addresses that are returned |
| 52 | +/// by system calls. You can use the `init(minimumCapacity:)` initializer |
| 53 | +/// to create an empty socket address with the specified storage capacity, |
| 54 | +/// then you can pass it to functions like `.accept(client:)` to retrieve |
| 55 | +/// addresses without repeatedly allocating memory. |
| 56 | +/// |
| 57 | +/// `SocketAddress` is able to hold any IPv4 or IPv6 address without allocating |
| 58 | +/// any memory. For other address families, it may need to heap allocate a |
| 59 | +/// storage buffer, depending on the size of the stored value. |
| 60 | +/// |
| 61 | +/// The corresponding C type is `sockaddr_t`. |
16 | 62 | public struct SocketAddress {
|
17 | 63 | internal var _variant: _Variant
|
18 | 64 |
|
|
0 commit comments