Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sockets

Source: lib/net.x

Guide: Sockets and HTTP

Blocking TCP sockets backed by File streams.

Include it with #include "net.x".

Primary API

FunctionSummary
Listener.acceptAccepts a connection, or returns NULL after Listener.stop.
Listener.bindBinds a TCP listener.
Listener.closeReleases the listener.
Listener.portReturns the actual bound port, including when bind requested port zero.
Listener.stopRequests stopping from any thread.
Socket.adoptTakes ownership of fd, including on failure, and creates separate File streams.
Socket.closeCloses both streams.
Socket.connectConnects to host, trying resolved addresses in order.
Socket.peerReturns numeric host:port text, with IPv6 in brackets.
Socket.set_timeoutSets receive and send timeouts in milliseconds.
Socket.shutdown_writeFlushes output and sends EOF to the peer while retaining input.

Listener

Listener.accept

Socket Listener.accept(Listener listener)

Accepts a connection, or returns NULL after Listener.stop. Thread.stop transfers its cooperative cancellation cause, like a Channel wait.

Raises: <bad-arg> for NULL, or <io-fail> on a native accept or poll failure.

Source: lib/net.x

Listener.bind

Listener Listener.bind(String host, int port)

Binds a TCP listener. Port zero selects an available port; a null host binds passive addresses. IPv6 listeners request dual-stack operation. The first bind or connect ignores SIGPIPE process-wide.

Raises: <bad-arg> for an invalid port, <not-found> on resolution failure, or <io-fail> with operation and errno on a host failure.

Source: lib/net.x

Listener.close

void Listener.close(Listener listener)

Releases the listener. Call after its accepting thread has finished. Repeated calls are harmless.

Source: lib/net.x

Listener.port

int Listener.port(Listener listener)

Returns the actual bound port, including when bind requested port zero.

Raises: <bad-arg> for NULL.

Source: lib/net.x

Listener.stop

void Listener.stop(Listener listener)

Requests stopping from any thread. A blocked accept returns within one poll interval. The listener descriptor remains owned until close.

Raises: <bad-arg> for NULL.

Source: lib/net.x

Socket

Socket.adopt

Socket Socket.adopt(int fd)

Takes ownership of fd, including on failure, and creates separate File streams. The descriptor must name a connected TCP socket. This operation enables TCP_NODELAY.

Raises: <io-fail> on a host failure.

Source: lib/net.x

Socket.close

void Socket.close(Socket socket)

Closes both streams. Repeated calls are harmless. Close, like File.close, discards the close result; flush explicitly to report write errors.

Source: lib/net.x

Socket.connect

Socket Socket.connect(String host, int port, int timeout_ms)

Connects to host, trying resolved addresses in order. A positive timeout bounds each connection attempt in milliseconds; zero waits indefinitely. The returned connection uses blocking File operations. The first bind or connect ignores SIGPIPE process-wide.

Raises: <bad-arg> for a null host, invalid port, or negative timeout, <not-found> on resolution failure, or <io-fail> with errno.

Source: lib/net.x

Socket.peer

String Socket.peer(Socket socket)

Returns numeric host:port text, with IPv6 in brackets.

Raises: <bad-arg> for NULL.

Source: lib/net.x

Socket.set_timeout

void Socket.set_timeout(Socket s, int ms)

Sets receive and send timeouts in milliseconds. Zero disables timeouts. A timed-out File operation raises <io-fail> with EAGAIN or EWOULDBLOCK.

Raises: <bad-arg> for NULL or negative milliseconds, or <io-fail>.

Source: lib/net.x

Socket.shutdown_write

void Socket.shutdown_write(Socket s)

Flushes output and sends EOF to the peer while retaining input.

Raises: <bad-arg> for NULL, or <io-fail> if flushing or shutdown fails.

Source: lib/net.x

Advanced and interop API

FunctionSummary
Listener.cleanupReleases the listener when a managed local leaves its block.
Listener.write_reprWrites the listener’s fields, reading its stop state atomically.
Socket.cleanupReleases both streams when a managed local leaves its block.

Listener

Listener.cleanup

void Listener.cleanup(Listener listener)

Releases the listener when a managed local leaves its block.

Source: lib/net.x

Listener.write_repr

Buffer Listener.write_repr(Listener listener, Buffer out)

Writes the listener’s fields, reading its stop state atomically.

Source: lib/net.x

Socket

Socket.cleanup

void Socket.cleanup(Socket socket)

Releases both streams when a managed local leaves its block.

Source: lib/net.x

Public types

TypeKindSummary
ListenerclassAn owned TCP listener.
SocketclassAn owned TCP connection.

Listener

class Listener struct { int fd; String host; int port, stopped; } *

An owned TCP listener. stop may be called from another thread; accept and close each have one owner.

Source: lib/net.x

Socket

class Socket struct { int fd; File input, output; String peer; } *

An owned TCP connection. Use ordinary File operations on input and output, and flush output at message boundaries. The socket’s Scope or close releases both streams.

Source: lib/net.x

Design notes

Each socket owns separate input and output streams because stdio update streams require positioning between reading and writing. Listener.stop and Thread.stop are observed within one 50 ms poll interval. Close the listener only after its accepting thread has finished.