Net.Socket
module Net.Socket
Process-owned TCP byte streams and listeners. All blocking operations return typed NetError values; close operations are idempotent.
using Net
using Net.Socket
let listener = TCP.listen(TCP.Endpoint.loopback(Port.from(0).try)).try
let address = listener.localAddress.try
let client = TCP.connect(address).try
client.sendAll("ping".to(Binary).try).try
listener.close
module Net.Socket.TCP
connect
connect(endpoint: Endpoint) -> Result<TCPConnection, NetError>
connect(endpoint: Endpoint, options: ConnectOptions) -> Result<TCPConnection, NetError>
Connects to a TCP endpoint with the backend's bounded connect deadline.
Returns a stream or Connect/Timeout
Examples
+TCP.connect(TCP.Endpoint.host("example.test", Port.from(80).try))+.
listen
listen(endpoint: Endpoint) -> Result<TCPListener, NetError>
listen(endpoint: Endpoint, options: ListenOptions) -> Result<TCPListener, NetError>
Binds and starts listening. Port zero selects an ephemeral local port.
Returns a listener or Connect
Examples
+TCP.listen(TCP.Endpoint.loopback(Port.from(0).try))+.
sendAll
sendAll(connection: TCPConnection, data: Binary) -> Result<Integer, NetError>
Sends every byte or reports the failure and transfer progress.
receiveChunk
receiveChunk(connection: TCPConnection, limit: Integer, timeout: Duration) -> Result<Binary, NetError>
Receives one bounded chunk; EOF is reported as Closed.
receiveExactly
receiveExactly(connection: TCPConnection, count: Integer, timeout: Duration) -> Result<Binary, NetError>
Receives exactly count bytes or returns a typed EOF/timeout failure.
receiveUntil
receiveUntil(connection: TCPConnection, delimiter: Binary, limit: Integer, timeout: Duration) -> Result<Binary, NetError>
Receives through the first delimiter without exceeding limit bytes.
The delimiter is included in the returned bytes. An empty delimiter is a Parse error; reaching the bound first is a Limit error.
Examples
+connection.receiveUntil("\r\n\r\n".to(Binary).try, 65536).try+
receiveLine
receiveLine(connection: TCPConnection, limit: Integer, timeout: Duration) -> Result<Binary, NetError>
Receives through a newline without exceeding limit bytes.
shutdownWrite
shutdownWrite(connection: TCPConnection) -> Result<Void, NetError>
Half-closes the write side while leaving reads available.
accept
accept(listener: TCPListener, timeout: Duration) -> Result<TCPConnection, NetError>
Waits for and returns the next connection.
localAddress
localAddress(connection: TCPConnection) -> Result<Endpoint, NetError>
Returns the bound local endpoint, including an ephemeral assigned port.
peerAddress
peerAddress(connection: TCPConnection) -> Result<Endpoint, NetError>
Returns the remote endpoint of a connected stream.
type Plain
Marker for an unencrypted stream.
type TCPConnection
An opaque connected TCP stream.
type TCPListener
An opaque TCP listening socket.
record Endpoint
A host name or numeric address paired with a validated port.
Fields
hostStringportNet.Port
record ConnectOptions
Fields
record ListenOptions
Fields
module Net.Socket.TCP.Endpoint
module Net.Socket.UDP
bind
bind(endpoint: Endpoint) -> Result<Socket, NetError>
bind(endpoint: Endpoint, options: BindOptions) -> Result<Socket, NetError>
Binds a datagram socket. Port zero selects an ephemeral local port.
sendTo
sendTo(socket: Socket, endpoint: Endpoint, data: Binary) -> Result<Integer, NetError>
Sends one complete datagram and returns its byte count.
receiveFrom
receiveFrom(socket: Socket, limit: Integer) -> Result<Datagram, NetError>
Receives one datagram no larger than limit bytes.
localAddress
localAddress(socket: Socket) -> Result<Endpoint, NetError>
Returns the bound endpoint, including an ephemeral assigned port.
type Socket
Connectionless datagrams. A receive limit rejects an oversized datagram instead of returning a silently truncated payload.
let socket = UDP.bind(UDP.Endpoint.loopback(Port.from(0).try)).try
let address = socket.localAddress.try
socket.sendTo(address, "hello".to(Binary).try).try
let packet = socket.receiveFrom(1024).try
socket.close
An opaque bound datagram socket.
record Endpoint
A datagram address paired with a validated port.
Fields
hostStringportNet.Port
record Datagram
A received datagram and its source endpoint.
Fields
sourceEndpointdataBinary
record BindOptions
Curated socket policy. Broadcast is opt-in. The receive timeout applies to each receiveFrom call; multicast TTL is bounded to the IP hop-limit range.
Fields
module Net.Socket.UDP.Endpoint
module Net.Socket.Unix
connect
connect(address: Address) -> Result<UnixConnection, NetError>
connect(address: Address, options: ConnectOptions) -> Result<UnixConnection, NetError>
Connects to a filesystem-domain listener.
listen
listen(address: Address) -> Result<UnixListener, NetError>
listen(address: Address, options: ListenOptions) -> Result<UnixListener, NetError>
Binds a new path; an existing filesystem entry is never removed implicitly.
sendAll
sendAll(connection: UnixConnection, data: Binary) -> Result<Integer, NetError>
Sends every byte and returns the count.
receiveChunk
receiveChunk(connection: UnixConnection, limit: Integer) -> Result<Binary, NetError>
Receives one bounded chunk; EOF is Closed.
receiveExactly
receiveExactly(connection: UnixConnection, count: Integer) -> Result<Binary, NetError>
Receives exactly count bytes or returns a typed EOF/timeout failure.
receiveUntil
receiveUntil(connection: UnixConnection, delimiter: Binary, limit: Integer) -> Result<Binary, NetError>
Receives through delimiter, including it, within an explicit bound.
Examples
+connection.receiveUntil("\n".to(Binary).try, 4096).try+
receiveLine
receiveLine(connection: UnixConnection, limit: Integer) -> Result<Binary, NetError>
Receives through a newline without exceeding limit bytes.
shutdownWrite
shutdownWrite(connection: UnixConnection) -> Result<Void, NetError>
Half-closes the write side while leaving reads available.
accept
accept(listener: UnixListener) -> Result<UnixConnection, NetError>
Waits for and returns the next stream connection.
type UnixConnection
Filesystem-domain streams for local IPC. The listener owns and removes only the socket path it successfully created.
let address = Unix.Address.path("/tmp/my-service.sock").try
let listener = Unix.listen(address).try
let client = Unix.connect(address).try
listener.close
An opaque connected filesystem-domain byte stream.
type UnixListener
An opaque filesystem-domain stream listener.
record Address
A validated absolute filesystem socket path.
Fields
pathString
Defined in other modules
record ConnectOptions
Fields
record ListenOptions
Fields
module Net.Socket.Unix.Address
module Net.Socket.TLS
connect
connect(endpoint: Net.Socket.TCP.Endpoint, config: ClientConfig) -> Result<TLSConnection, NetError>
Opens a direct TLS connection with a bounded handshake deadline.
Returns a TLS stream or typed failure
sendAll
sendAll(connection: TLSConnection, data: Binary) -> Result<Integer, NetError>
Sends every plaintext byte through the TLS stream.
receiveChunk
receiveChunk(connection: TLSConnection, limit: Integer) -> Result<Binary, NetError>
Receives one decrypted chunk no larger than limit.
closed?
closed?(connection: TLSConnection) -> Bool
Returns whether the TLS connection owner has stopped
type TLSConnection
TLS client streams. Certificate and hostname verification are enabled by default; disabling verification must be an explicit configuration choice.
let endpoint = TCP.Endpoint.host("example.test", Port.from(443).try)
let tls = TLS.connect(endpoint, TLS.ClientConfig {
serverName: "example.test"
}).try
tls.close
An opaque verified or explicitly unverified TLS byte stream.
record ClientConfig
Client handshake policy. TLS 1.2/1.3 are enabled; verification defaults on.