Module std/net.vv
The net module provides system-level network connection capabilities.
Example:
import net from "std/net.vv"
import console from "std/console.vv"
import text from "std/io/text.vv"
import utf8 from "std/encoding/utf8.vv"
let client = try net.dial("tcp", "127.0.0.1:8080")
defer net.close(client)
let r = try text.reader(try net.reader(client), utf8)
// ...
Table of Contents
Exports
fun dial(network, address)
extern "native"
Connects to a network address.
network: The network protocol (e.g., "tcp", "unix").address: The address to connect to.
Returns the connection object wrapped in a result.
fun listen(network, address)
extern "native"
Starts listening on a network address.
network: The network protocol (e.g., "tcp", "unix").address: The address to listen on.
Returns the listener object wrapped in a result.
fun accept(listener, handler)
extern "native"
Accepts incoming connections from a listener continuously in the background.
For each new connection, handler is called with the connection object.
listener: The listener object returned bylisten.handler: A function that receives each connectionfun(conn).
Returns None immediately. Connections continue to be accepted in the background while the rest of your program keeps running.
Warning: Do Not Modify Outer Variables Inside a Handler
Each handler call runs independently and at the same time as your main program. If a handler modifies a variable declared outside of it, the result is unpredictable — the value may be corrupted or the program may behave incorrectly.
Do not do this:
let counter = 0
fun handler(conn)
counter = counter + 1 // unsafe: may corrupt the value
end
Instead, use channel to send results back to the main program, or
use sync.mutex to safely protect a shared variable:
Safe pattern 1 — channel (recommended):
import channel from "std/channel.vv"
let ch = channel.make()
fun handler(conn)
// ... handle conn ...
channel.send(ch, "done")
end
accept(listener, handler)
let result = try channel.recv(ch, 5000)
Safe pattern 2 — mutex:
import sync from "std/sync.vv"
let mu = sync.mutex()
let counter = 0
fun handler(conn)
sync.lock(mu)
counter = counter + 1
sync.unlock(mu)
end
fun read(conn, n)
extern "native"
Reads bytes from a connection.
conn: The connection object.n: The number of bytes to read.
Returns a result containing a list of bytes.
fun write(conn, bytes)
extern "native"
Writes bytes to a connection.
conn: The connection object.bytes: The bytes (list of integers or characters) to write.
Returns a result containing the number of written bytes.
fun close(conn)
extern "native"
Closes a connection or listener.
conn: The connection or listener object to close.
fun set_read_timeout(conn, timeout_ms)
extern "native"
Sets the read timeout for a connection.
conn: The connection object.timeout_ms: The timeout in milliseconds (0 disables the timeout).
fun set_write_timeout(conn, timeout_ms)
extern "native"
Sets the write timeout for a connection.
conn: The connection object.timeout_ms: The timeout in milliseconds (0 disables the timeout).
fun reader(conn)
Creates a generalized reader stream for a connection.
conn: The connection object.
Returns a record with conn, read, and close functions.
fun writer(conn)
Creates a generalized writer stream for a connection.
conn: The connection object.
Returns a record with conn, write, and close functions.