net

Low-level networking: TCP and UDP sockets, TLS, DNS lookups, and an embedded HTTP client. Import it with import net.

Connections, listeners, and UDP sockets are referenced by integer handles returned from connect, listen, and accept. A handle of -1 means the operation failed. Close handles with the matching close function when done.

net.tcp

FunctionSignatureDescription
connectnet.tcp.connect(host: string, port: int) → intDial a TCP connection; returns a handle or -1
connectTLSnet.tcp.connectTLS(host: string, port: int) → intDial a TLS connection
listennet.tcp.listen(host: string, port: int) → intOpen a listener; returns a listener handle
listenTLSnet.tcp.listenTLS(host: string, port: int, certPEM: string, keyPEM: string) → intOpen a TLS listener with the given cert and key
acceptnet.tcp.accept(listenerId: int) → intAccept the next connection; returns a connection handle
sendnet.tcp.send(id: int, data: string) → boolWrite data to a connection
recvnet.tcp.recv(id: int, maxBytes: int) → stringRead up to maxBytes (empty string on EOF/error)
closenet.tcp.close(id: int) → boolClose a connection or listener
setNoDelaynet.tcp.setNoDelay(id: int, noDelay: bool) → boolToggle TCP_NODELAY
setKeepAlivenet.tcp.setKeepAlive(id: int, keepAlive: bool) → boolToggle TCP keep-alive

net.socket is an alias for net.tcp with the same API.

net.udp

FunctionSignatureDescription
connectnet.udp.connect(host: string, port: int) → intOpen a connected UDP socket
listennet.udp.listen(host: string, port: int) → intOpen a UDP socket bound to an address
sendnet.udp.send(id: int, data: string) → boolSend a datagram
recvnet.udp.recv(id: int, maxBytes: int) → stringReceive up to maxBytes
closenet.udp.close(id: int) → boolClose the socket

net.dns

FunctionSignatureDescription
resolvenet.dns.resolve(host: string) → stringFirst resolved address (empty on failure)
resolveAllnet.dns.resolveAll(host: string) → string[]All resolved addresses
lookupMXnet.dns.lookupMX(host: string) → string[]MX records as "host pref" strings
lookupTXTnet.dns.lookupTXT(host: string) → string[]TXT records
lookupNSnet.dns.lookupNS(host: string) → string[]NS record hostnames
reversenet.dns.reverse(ip: string) → stringReverse-DNS the IP to a hostname

net.http

An HTTP client. get, post, put, patch, delete, and request return the response body; head and status return the status code. See the dedicated http module for the same client surface.

FunctionSignatureDescription
getnet.http.get(url: string) → stringGET, returns body
postnet.http.post(url: string, body: string) → stringPOST with JSON body
putnet.http.put(url: string, body: string) → stringPUT with JSON body
patchnet.http.patch(url: string, body: string) → stringPATCH with JSON body
deletenet.http.delete(url: string) → stringDELETE, returns body
headnet.http.head(url: string) → intHEAD, returns status code
statusnet.http.status(url: string) → intGET, returns status code only
requestnet.http.request(method: string, url: string, body: string, headers: string[]) → stringGeneric request with custom "Key: Value" headers

Top-level shortcuts

net also exposes flat helpers operating on the same handle registry.

FunctionSignatureDescription
connectnet.connect(host: string, port: int) → intDial a TCP connection
connectTLSnet.connectTLS(host: string, port: int) → intDial a TLS connection
listennet.listen(host: string, port: int) → intOpen a TCP listener
acceptnet.accept(listenerId: int) → intAccept a connection
sendnet.send(id: int, data: string) → boolWrite data
sendLinenet.sendLine(id: int, line: string) → boolWrite data followed by CRLF
recvnet.recv(id: int, maxBytes: int) → stringRead up to maxBytes
recvLinenet.recvLine(id: int) → stringRead one CRLF-terminated line (trimmed)
recvTimeoutnet.recvTimeout(id: int, ms: int, maxBytes: int) → stringRead with a millisecond deadline
setTimeoutnet.setTimeout(id: int, ms: int) → boolSet a read/write deadline (0 clears it)
closenet.close(id: int) → boolClose a connection, listener, or UDP socket
localAddrnet.localAddr(id: int) → stringLocal address of the handle
remoteAddrnet.remoteAddr(id: int) → stringRemote address of a connection
connectUDPnet.connectUDP(host: string, port: int) → intOpen a connected UDP socket
listenUDPnet.listenUDP(host: string, port: int) → intBind a UDP socket
sendUDPnet.sendUDP(id: int, data: string) → boolSend a datagram
recvUDPnet.recvUDP(id: int, maxBytes: int) → stringReceive a datagram
resolvenet.resolve(host: string) → stringFirst resolved address
resolveAllnet.resolveAll(host: string) → string[]All resolved addresses

Example

import net

// simple TCP client
let conn = net.tcp.connect("example.com", 80)
if conn != -1 {
  net.sendLine(conn, "GET / HTTP/1.0")
  net.sendLine(conn, "Host: example.com")
  net.sendLine(conn, "")
  let line = net.recvLine(conn)   // first response line
  print(line)
  net.tcp.close(conn)
}

// DNS lookup
print(net.dns.resolve("example.com"))
Standard library · View as Markdown · llms-full.txt