<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "Event",
  "identifier" : "/documentation/Event/Socket/write(_:timeout:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Event"
    ],
    "preciseIdentifier" : "s:5Event6SocketC5write_7timeouty10Foundation4DataV_s8DurationVSgtYaKF"
  },
  "title" : "write(_:timeout:)"
}
-->

# write(_:timeout:)

Writes all bytes in `data` to the socket, awaiting write-readiness.

```
func write(_ data: Data, timeout: Duration? = nil) async throws
```

## Parameters

`data`

The bytes to send.

`timeout`

Maximum time to wait for write-readiness. `nil` (default) waits
indefinitely. On expiry the call throws [`SocketError.timeout`](/documentation/Event/SocketError/timeout) without
having written any bytes.

## Discussion

Registers an `EV_WRITE` event and drives the loop until the descriptor is
writable, then issues a single `write(2)` syscall covering the full buffer.

> Warning: This implementation does **not** handle partial writes — if the
> kernel accepts fewer bytes than requested the remainder is silently
> discarded (the callback just reports success). In practice, small writes
> on connected TCP sockets complete in one syscall on all supported platforms,
> but callers writing large buffers should chunk explicitly until the
> planned backpressure helper lands. See <doc://Event/documentation/Event/ProductionConsiderations>.

> Throws: ``doc://Event/documentation/Event/SocketError/writeFailed(_:)`` with the errno payload if
> `write(2)` returns a negative value (e.g. `EPIPE` when the peer closed),
> or ``doc://Event/documentation/Event/SocketError/timeout`` if `timeout` elapses first.