Sends data on a connection.


func nw_connection_send(_ connection: nw_connection_t, _ content: __DispatchData?, _ context: nw_content_context_t, _ is_complete: Bool, _ completion: @escaping nw_connection_send_completion_t)



The data to send on the connection. May be nil if this send marks its context as complete, such as by sending NW_CONNECTION_FINAL_MESSAGE_CONTEXT as the context and marking the send complete to send a write-close.


The context associated with the content, which represents a logical message to be sent on the connection. All content sent within a single context will be sent as an in-order unit, up until the point that the context is marked complete. Once a context is marked complete, it may be re-used as a new logical message. Protocols like TCP that cannot send multiple independent messages at once (serial bytestreams) will only start processing a new context once the prior context has been marked complete. Defaults to NW_CONNECTION_DEFAULT_MESSAGE_CONTEXT.


A flag indicating if the caller's sending context (logical message) is now complete. Until a context is marked complete, content sent for other contexts may not be sent immediately (if the protocol requires sending bytes serially, like TCP). For datagram protocols, like UDP, this flag indicates that the content represents a complete datagram.

When sending using streaming protocols like TCP, this flag can be used to mark the end of a single message on the stream, of which there may be many. However, it can also indicate that the connection should send a "write close" (a TCP FIN) if the sending context is the final context on the connection. Specifically, to send a "write close", pass NW_CONNECTION_FINAL_MESSAGE_CONTEXT or NW_CONNECTION_DEFAULT_STREAM_CONTEXT for the context (or create a custom context and call nw_content_context_set_is_final(_:_:)), and mark the send as complete.


A completion handler to notify the caller when content has been processed by the connection, or a marker that this data is idempotent (NW_CONNECTION_SEND_IDEMPOTENT_CONTENT) and may be sent multiple times as fast open data if nw_parameters_set_fast_open_enabled(_:_:) was called.

See Also

Sending and Receiving Data

typealias nw_connection_send_completion_t

A completion handler that indicates when the connection has finished processing sent content.

typealias nw_content_context_t

A representation of a message to send or receive, containing protocol metadata and send properties.

func nw_connection_receive(nw_connection_t, UInt32, UInt32, nw_connection_receive_completion_t)

Schedules a single receive completion handler, with a range indicating how many bytes the handler can receive at one time.

typealias nw_connection_receive_completion_t

A completion handler that indicates when content has been received by the connection, or that an error was encountered.

func nw_connection_receive_message(nw_connection_t, nw_connection_receive_completion_t)

Schedules a single receive completion handler for a complete message, as opposed to a range of bytes.

func nw_connection_batch(nw_connection_t, () -> Void)

Defines a block in which calls to send and receive are processed as a batch to improve performance.

func nw_connection_get_maximum_datagram_size(nw_connection_t) -> UInt32

Accesses the maximum size of a datagram message that can be sent on a connection.