Files
2026-03-09 23:09:46 -04:00

53 lines
1.6 KiB
Clojure

(ns gen_tcp
"Erlang :gen_tcp module — TCP socket interface.
In CljElixir: (gen_tcp/listen port opts), (gen_tcp/accept socket), etc.
Core networking module for TCP servers and clients on the BEAM.")
(defn listen
"Starts listening on `port`. Returns {:ok listen-socket} or {:error reason}.
(gen_tcp/listen 4000 [:binary {:active false} {:reuseaddr true}])"
[port opts])
(defn accept
"Accepts an incoming connection. Blocks until a connection arrives.
(gen_tcp/accept listen-socket) ;=> {:ok socket}
(gen_tcp/accept listen-socket 5000) ;=> with 5s timeout"
([listen-socket])
([listen-socket timeout]))
(defn connect
"Connects to a TCP server. Returns {:ok socket} or {:error reason}.
(gen_tcp/connect \"localhost\" 4000 [:binary {:active false}])
(gen_tcp/connect {127 0 0 1} 4000 opts 5000) ;=> with timeout"
([address port opts])
([address port opts timeout]))
(defn send
"Sends data over a TCP socket. Returns :ok or {:error reason}.
(gen_tcp/send socket \"hello\")"
[socket packet])
(defn recv
"Receives data from a socket. Blocks until data arrives.
(gen_tcp/recv socket 0) ;=> {:ok data} (0 = any amount)
(gen_tcp/recv socket 1024 5000) ;=> with timeout"
([socket length])
([socket length timeout]))
(defn close
"Closes a TCP socket.
(gen_tcp/close socket) ;=> :ok"
[socket])
(defn controlling-process
"Transfers socket ownership to another process.
(gen_tcp/controlling-process socket new-owner-pid) ;=> :ok"
[socket pid])
(defn shutdown
"Shuts down one or both directions of a socket.
(gen_tcp/shutdown socket :write) ;=> :ok
(gen_tcp/shutdown socket :read-write)"
[socket how])