March Docs

Socket

TCP socket module: a clean interface over the tcp_* builtins.

Wraps the low-level tcp_connect / tcp_send_all / tcp_recv_all / tcp_close builtins with an ergonomic Result-based API. All operations are synchronous. For async use, wrap in task_spawn.

File descriptors are plain Int values. Always call Socket.close when done, ideally via Socket.with_connection which ensures cleanup.

Public API: Socket.connect(host, port) : Result(Int, SocketError) Socket.write(fd, data) : Result((), SocketError) Socket.recv(fd, max_bytes) : Result(String, SocketError) Socket.recv_timeout(fd, max, ms) : Result(String, SocketError) Socket.close(fd) : () Socket.with_connection(host, port, f) : Result(a, SocketError) Socket.error_message(e) : String

Types

typeSocketErrorSocketError#

Functions

fncloseclose(fd : Int) : ()#

Close fd. Silently ignores errors (already-closed fds, etc.).

fnconnectconnect(host : String, port : Int) : Result(Int, SocketError)#

Open a TCP connection to host:port. Returns Ok(fd) or Err.

fnerror_messageerror_message(e : SocketError) : String#

Return a human-readable description of a SocketError.

fnrecvrecv(fd : Int, max_bytes : Int) : Result(String, SocketError)#

Receive up to max_bytes from fd (no timeout). Returns Ok(data) or Err.

fnrecv_timeoutrecv_timeout(fd : Int, max_bytes : Int, _timeout_ms : Int) : Result(String, SocketError)#

Receive up to max_bytes from fd with timeout_ms timeout (0 = no timeout).

fnwith_connectionwith_connection(host : String, port : Int, f)#

Open a connection, run f(fd), then close. fd is always closed.

fnwritewrite(fd : Int, data : String) : Result((), SocketError)#

Write all bytes of data over fd. Returns Ok(()) or Err(WriteFailed(...)).