rontolisp:tcp-listen
(rontolisp:tcp-listen port &optional host)
Binds a listening TCP socket on port and returns a listener handle for
rontolisp:tcp-accept and close.
Port 0 picks a free ephemeral port — read the actual port back with
rontolisp:tcp-local-port. Without host the
listener binds all interfaces; pass an address string (for example
"127.0.0.1") to bind only one.
A server accepts connections in a loop; each accepted handle is a
bidirectional stream (see
rontolisp:tcp-accept):
(let ((listener (rontolisp:tcp-listen 7777)))
(do ((n 1 (+ n 1))) (nil)
(let ((sock (rontolisp:tcp-accept listener)))
(do ((line (read-line sock) (read-line sock)))
((null line) (close sock))
(write-line line sock))))) ; echo every client forever
Backend support
- Interpreter and JVM: use the JDK
java.net.ServerSocket(withSO_REUSEADDRon the interpreter); a failed bind (for example a port already in use) signals an error. Aportoutside(INTEGER 0 65535)or ahostthat is neither a string nornil(every interface) signals the operator'stype-errorbefore anything binds. - WASM: component-only, over
wasi:sockets@0.3.0.hostmay be an IPv4 literal or a hostname, resolved asrontolisp:tcp-connectresolves one (add-S allow-ip-name-lookup=y). Compile with--componentand run withwasmtime run -S inherit-network=y(wasmtime 46+); a failed bind returnsnil. Call-time error in Preview 1 (core-module) mode. - Native executable (
--native): as on the interpreter and the JVM -- the runner binds the port,hostmay be a hostname or an IP literal, and a failed bind signals the same error (TCP sockets in a native executable). - Browser playground: not supported (no raw TCP in the browser sandbox).
Limitations
- The listener handle only supports
rontolisp:tcp-accept,rontolisp:tcp-local-portandclose— it is not a byte stream itself. - See
rontolisp:tcp-connectfor the shared socket limitations (TCP only, IPv4 only on WASM). The listener serves plain TCP; for a TLS listener userontolisp:tls-listen(interpreter/JVM only).