net module
Use TCP, UDP, and supported TLS sockets for lower-level network I/O.
Import it with 'use net'.
use net
print(count(net.resolve('127.0.0.1', 80)) > 0)
true
Requires libuv and network permission. TCP streams and UDP datagrams use the core read and write operations. Close owned sockets with net.close. Listen or bind to port 0 to allocate an ephemeral port. An address record contains address (string) and port (i64). UDP receives include data and sender address information. TCP reads return packed chunks, with empty bytes at EOF. The default read size is 8192 for TCP and 65536 for UDP. A UDP buffer that is too small can cause an error. A read may split UTF-8 characters or application messages. Accumulate bytes and decode or frame at the application boundary.
| Socket option | Accepted value and default | Effect |
|---|---|---|
protocol |
tcp (default) or udp | Selects transport. bind forces UDP. |
timeout |
Nonnegative seconds or null (default) | Sets the socket deadline for operations. Zero requests an immediate operation and requires numeric host resolution. |
nonblocking |
Boolean, default false | Sets subsequent socket operation timeout to zero after setup. |
nodelay |
Boolean, default true | Enables TCP_NODELAY on clients and accepted connections. |
backlog |
Positive signed integer up to C INT_MAX, default 128 | Pending TCP accept queue. |
tls |
Boolean or null, default false | Clients opt into verified TLS over TCP. Requires TLS support. |
ca |
Readable CA bundle path | Optional client trust override, requiring filesystem permission. |
certificate, key |
Readable certificate chain and private key paths | Enable TLS on a TCP listener by supplying the certificate and its key. Requires TLS and filesystem permission. |
TLS verifies server names or IP addresses and uses platform trust defaults when no CA is specified. On Windows the default trust comes from the system root store. Accept and poll allow an explicit timeout. Core socket read and write use the configured timeout. Most immediate would-block and deadline failures raise socket errors. Poll instead reports false. See TCP client and server and UDP communication for complete exchanges.