Async Net API Reference

Async TCP/UDP networking with non-blocking operations

Import

U std/async_net

Overview

The async_net module provides high-level async TCP and UDP networking APIs built on POSIX sockets with non-blocking I/O. It includes socket creation, address construction, and low-level socket operations.

Constants

Socket Types

ConstantValueDescription
AF_INET2IPv4 address family
SOCK_STREAM1TCP socket
SOCK_DGRAM2UDP socket
IPPROTO_TCP6TCP protocol
IPPROTO_UDP17UDP protocol

Socket Options

ConstantValue (macOS/Linux)Description
SOL_SOCKET65535 / 1Socket level options
SO_REUSEADDR2Allow address reuse
SO_BROADCAST6Allow broadcast
SO_KEEPALIVE8Enable keepalive

Non-blocking I/O

ConstantValueDescription
F_GETFL3Get file flags
F_SETFL4Set file flags
O_NONBLOCK4Non-blocking mode
EAGAIN35Would block
EWOULDBLOCK35Would block
EINPROGRESS36Connection in progress

Buffer Sizes

ConstantValueDescription
DEFAULT_READ_BUF_SIZE8192Default read buffer
MAX_UDP_PACKET65536Maximum UDP packet size

Helper Functions

make_sockaddr_in

F make_sockaddr_in(host: str, port: i64) -> i64

Create a sockaddr_in structure for the given host and port. Returns a pointer to the structure, or 0 on error.

Extern Functions

The module exposes POSIX socket system calls: socket, bind, listen, accept, connect, send, recv, sendto, recvfrom, close, setsockopt, getsockopt, inet_pton, htons, ntohs, fcntl.

Example

U std/async_net

F main() {
    # Create a TCP socket
    fd := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)

    # Create address
    addr := make_sockaddr_in("127.0.0.1", 8080)

    # Bind and listen
    bind(fd, addr, SOCKADDR_IN_SIZE)
    listen(fd, 128)
}