Skip to main content

std.net — Overview

std.net is the networking surface. Every object is event-driven: you install on_* callbacks and the engine invokes them on background tasks when data arrives. The calling script must stay alive (std.async.sleep / await) or the callbacks never fire.

:::note TLS TCP, UDP, HTTP, and WebSocket transmit in plaintext by default. TLS is available when the engine is built with OpenSSLtcp_client.connect(..., {tls: true}), tcp_server.listen(..., {cert, key}), http_server.listen(..., {cert, key}), https:// request URLs, and wss:// websockets all work on OpenSSL builds. Without OpenSSL they throw UNSUPPORTED (see Errors). :::

Mental model

  1. Create an object: std.net.tcp_server(), udp(), http_client(), etc.
  2. Install handlers with on_connect, on_receive, on_message, …
  3. Start it: listen, connect, bind, or request.
  4. Keep the script alive — e.g. std.async.sleep(5000) — so background fibers can run.
  5. Stop/close when done: stop(), close().

All network work runs through the same fiber pool as std.async and std.time timers — failures in connection-oriented callbacks (TLS handshake, background errors) are recorded on the owning object and polled via last_error() (tcp_client, tcp_server, udp, websocket, http_server — see Concurrency — Errors).

Modules

PageWhat it covers
TCPtcp_client / tcp_server — message-based, on_connect/on_receive/on_disconnect
UDPudp — connectionless bind + send_to
HTTP Clienthttp_client — single request with headers and http_codes
HTTP Serverhttp_serverroutes + handlers returning string or response object
WebSocketwebsocket — text/binary frames, on_message/on_close
URLparse_url plus the url sub-module — parse, build, percent/form encode
DNSdns_lookup — hostname → IP array

Each page is self-contained with runnable examples and a method table. Start with TCP for the core event-driven pattern, then pick the protocol you need. See Concurrency for sleep/await that keeps these servers alive.