vera:net
TCP/UDP socket operations.
import "vera:net"
Overview
Provides portable socket operations for TCP and UDP clients and servers. All functions are built on compiler primitives (__vera_sys_*) which the compiler maps to platform-specific networking APIs (POSIX sockets on Linux/macOS, Winsock on Windows).
Dependencies: None (uses primitives directly)
Constants
These are VERA-portable values. The compiler maps them to platform equivalents.
| Constant | Value | Description |
|---|---|---|
AF_INET | 2 | IPv4 address family |
SOCK_STREAM | 1 | TCP (stream) socket type |
SOCK_DGRAM | 2 | UDP (datagram) socket type |
SHUT_READ | 0 | Shutdown reading |
SHUT_WRITE | 1 | Shutdown writing |
SHUT_BOTH | 2 | Shutdown both reading and writing |
Functions
Socket Creation
tcp_socket
function tcp_socket() -> i32
Create a TCP (stream) socket using IPv4.
Returns: socket file descriptor, or -1 on error
udp_socket
function udp_socket() -> i32
Create a UDP (datagram) socket using IPv4.
Returns: socket file descriptor, or -1 on error
Client Operations
tcp_connect
function tcp_connect(fd: i32, ip: ptr, ip_len: i32, port: i32) -> i32
Connect a socket to an IPv4 address and port. The IP address must be a null-terminated string (e.g., "127.0.0.1\0").
Parameters:
fd(i32) — socket file descriptorip(ptr) — null-terminated IPv4 address stringip_len(i32) — length of the IP string including the null terminatorport(i32) — port number to connect to
Returns: 0 on success, -1 on error
Server Operations
tcp_bind
function tcp_bind(fd: i32, ip: ptr, ip_len: i32, port: i32) -> i32
Bind a socket to an IPv4 address and port. The IP address must be a null-terminated string; use "0.0.0.0\0" to bind to all interfaces.
Parameters:
fd(i32) — socket file descriptorip(ptr) — null-terminated IPv4 address stringip_len(i32) — length of the IP string including the null terminatorport(i32) — port number to bind to
Returns: 0 on success, -1 on error
tcp_listen
function tcp_listen(fd: i32, backlog: i32) -> i32
Start listening for incoming connections on a bound socket.
Parameters:
fd(i32) — socket file descriptor (must be bound)backlog(i32) — maximum length of the pending connection queue
Returns: 0 on success, -1 on error
tcp_accept
function tcp_accept(fd: i32) -> i32
Accept an incoming connection on a listening socket. Blocks until a connection arrives.
Parameters:
fd(i32) — listening socket file descriptor
Returns: new client socket file descriptor, or -1 on error
Data Transfer
socket_send
function socket_send(fd: i32, data: ptr, length: i64) -> i64
Send data on a connected socket.
Parameters:
fd(i32) — socket file descriptordata(ptr) — pointer to the data to sendlength(i64) — number of bytes to send
Returns: number of bytes sent, or -1 on error
socket_recv
function socket_recv(fd: i32, buffer: ptr, max_length: i64) -> i64
Receive data from a connected socket into a buffer.
Parameters:
fd(i32) — socket file descriptorbuffer(ptr) — pointer to the buffer to receive intomax_length(i64) — maximum number of bytes to receive
Returns: number of bytes received, 0 for EOF (peer closed connection), or -1 on error
Socket Management
socket_close
function socket_close(fd: i32) -> i32
Close a socket.
Parameters:
fd(i32) — socket file descriptor
Returns: 0 on success, or a negative value on error
socket_shutdown
function socket_shutdown(fd: i32, how: i32) -> i32
Shut down part of a socket connection.
Parameters:
fd(i32) — socket file descriptorhow(i32) — shutdown mode: 0 = stop reading, 1 = stop writing, 2 = stop both
Returns: 0 on success, or a negative value on error
socket_poll
function socket_poll(fd: i32, events: i32, timeout_ms: i32) -> i32
Poll a socket for readiness.
Parameters:
fd(i32) — socket file descriptorevents(i32) — bitmask of what to check: 1 = readable, 2 = writable, 3 = bothtimeout_ms(i32) — milliseconds to wait (-1 = block forever, 0 = non-blocking check)
Returns: bitmask of ready events (1 = readable, 2 = writable), 0 on timeout, -1 on error
set_reuseaddr
function set_reuseaddr(fd: i32) -> i32
Enable SO_REUSEADDR on a socket. Useful for servers to avoid "address already in use" errors when restarting.
Parameters:
fd(i32) — socket file descriptor
Returns: 0 on success