rontolisp:tcp-connect
(rontolisp:tcp-connect host port)
Opens a blocking TCP connection to host/port and returns a
bidirectional stream handle. The handle lives in the same handle space as
file streams, so the standard stream functions work on it directly:
read-line, write-line,
read-byte, write-byte and
close. Unlike buffered file output, socket writes are sent
immediately (write-line flushes per line). read-line returns nil once
the peer has closed the connection.
The example below is self-contained: it listens on an ephemeral port with
rontolisp:tcp-listen, connects to itself over the
loopback interface, and echoes one line through
rontolisp:tcp-accept:
A typical client connects to a fixed host and port and exchanges lines until the server closes:
(let ((sock (rontolisp:tcp-connect "127.0.0.1" 7777)))
(write-line "hello" sock)
(print (read-line sock)) ; the server's reply
(close sock))
Backend support
- Interpreter and JVM: use the JDK
java.net.Socket;hostmay be a hostname or an IP literal. A failed connection (for example a refused port) signals an error. - WASM: component-only, over
wasi:sockets@0.3.0(natively WASI 0.3 — unlikerontolisp:fetch, no 0.2 hybrid is needed).hostmust be an IPv4 literal such as"127.0.0.1"(hostname resolution viawasi:sockets/ip-name-lookupis not wired yet). Compile with--componentand run with-S tcp=y -S inherit-network=yon top of the async flags. A failed connection returnsnilinstead of a handle (the same nil-on-failure convention asrontolisp:fetch); without the-Sflags the component still starts, but every socket operation fails and yieldsnil. The tcp built-ins are a compile error in Preview 1 (core-module) mode. - Browser playground: not supported — the browser sandbox provides no raw TCP sockets, so every tcp function signals an error.
Limitations
- TCP only (no UDP yet): the connection is plain text. For an encrypted
client connection use
rontolisp:tls-connect(interpreter/JVM only). - The WASM component backend accepts IPv4 literals only, and a program cannot
combine
rontolisp:fetchand the tcp functions in one--componentbinary yet. read(the s-expression reader) does not work on socket handles; read lines or bytes and parse them explicitly (e.g. withread-from-string).