tcp_close
tcp_close(sock)
Input: sock · Output: no output
What it is
sock must be a TcpConn handle (from tcp_accept/tcp_connect) — not a TcpListener; calling it on a listener errors, since a listener has no explicit close and simply releases its OS socket when the handle is dropped. Shuts the connection down and marks it closed; returns none. Idempotent — closing an already-closed connection is a harmless no-op, not an error.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
tcp_close(sock) | sock TcpConn or TcpListenernot documented yet | no output | Shuts the connection down and marks it closed; returns none. Idempotent — closing an already-closed connection is a harmless no-op, not an error. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
function echo_once(listener)
conn = tcp_accept(listener) # blocks for the one client
msg = tcp_recv(conn)
tcp_send(conn, msg)
tcp_close(conn)
end function
listener = tcp_listen(0) # 0 -- let the OS pick a free port
port = tcp_port(listener)
w = spawn("echo_once", listener)
client = tcp_connect("127.0.0.1", port)
tcp_send(client, "hello")
print(tcp_recv(client)) # "hello" -- echoed back
tcp_close(client)
join(w)
hello
from concurrency
How it works
tcp_close(conn) shuts down both directions and releases the socket; closing an already-closed connection is a no-op, matching close(f)'s own idempotence above.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.