Binds the socket to a local address and port.
Must be called before Socket.listen for server sockets. Use host
"0.0.0.0" or "" to bind to all interfaces.
@eryx/_socket ModuleLow-level BSD socket API for Luau.
This module exposes the platform's native socket interface, providing
TCP and UDP networking with non-blocking I/O support. All data transfer
uses Luau buffer objects for zero-copy efficiency.
Caution
This is a low-level API. For HTTP, prefer using @eryx/http,
which handle framing, TLS, and connection management automatically.
local sock = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
sock:connect("127.0.0.1", 8080)
sock:sendAll(buffer.fromstring("GET / HTTP/1.0\r\n\r\n"))
local data = sock:recv(4096)
print(buffer.tostring(data))
sock:close()
Accepts an incoming connection, returning a new socket for the client along with the remote address and port.
Caution
This call blocks the current thread until a connection arrives (unless the socket is set to non-blocking mode via Socket.setblocking).
A nil return value indicates a non-blocking operation that would have blocked.
Receives a datagram of up to bufsize bytes along with the
sender's address (UDP).
A nil return value indicates a non-blocking operation that would have blocked.
Binds the socket to a local address and port.
Must be called before Socket.listen for server sockets. Use host
"0.0.0.0" or "" to bind to all interfaces.
The local address to bind to.
The port number to bind to.
Starts listening for incoming connections.
The socket must have been bound first. After calling this, use Socket.accept to accept individual connections.
Maximum number of queued connections. Defaults to a system-defined value (usually 128) when omitted.
Opens a connection to a remote host.
For TCP sockets this performs the full three-way handshake. In non-blocking mode the call may return before the connection is fully established.
The remote address to connect to.
The remote port number.
Closes the socket, releasing all associated OS resources.
After closing, no further operations may be performed on this socket.
It is safe to call close multiple times.
Shuts down one or both halves of the connection without closing the socket descriptor.
Use SHUT_RD (0) to stop reads, SHUT_WR (1) to stop writes,
or SHUT_RDWR (2) to stop both.
The shutdown mode - one of SHUT_RD, SHUT_WR, or SHUT_RDWR.
Sends data over the socket. Returns the number of bytes actually sent, which may be less than the full buffer length.
For guaranteed delivery of the entire buffer, use Socket.sendall instead.
A nil return value indicates a non-blocking operation that would have blocked.
The data to send.
Number of bytes sent.
Sends all data in the buffer, retrying internally until every byte has been transmitted or an error occurs.
In blocking operation, this function always returns true.
In non-blocking operation, this function returns false is the
operation would have blocked.
The data to send.
Sends data to a specific destination address (UDP).
Primarily used with SOCK_DGRAM sockets. Returns the number of
bytes actually sent.
A nil return value indicates a non-blocking operation that would have blocked.
The datagram payload to send.
The destination address.
The destination port.
Number of bytes sent.
Receives up to bufsize bytes from the socket.
Returns a buffer containing the received data, which may be shorter
than bufsize. An empty buffer (length 0) typically indicates the
remote end has closed the connection.
A nil return value indicates a non-blocking operation that would have blocked.
Maximum number of bytes to receive.
The received data.
Sets a socket option.
Common combinations:
SOL_SOCKET, SO_REUSEADDR, true - allow port reuseSOL_SOCKET, SO_KEEPALIVE, true - enable keep-aliveIPPROTO_TCP, TCP_NODELAY, true - disable Nagle's algorithmThe protocol level (e.g. SOL_SOCKET, IPPROTO_TCP).
The option identifier.
The value to set - a number or boolean depending on the option.
Gets the current value of a socket option.
The protocol level (e.g. SOL_SOCKET, IPPROTO_TCP).
The option identifier.
The current option value.
Switches the socket between blocking and non-blocking mode.
In non-blocking mode, operations like Socket.recv and Socket.send will raise an error instead of waiting when no data is available.
true for blocking, false for non-blocking.
Sets a timeout (in seconds) for blocking socket operations.
Pass nil or 0 to disable the timeout (wait indefinitely).
Timeout duration, or nil for no timeout.
Returns the remote address and port of the connected peer.
The remote IP address.
The remote port.
Returns the local address and port the socket is bound to.
The local IP address.
The local port.
Returns the underlying OS file descriptor / handle number for this socket. Useful for advanced I/O multiplexing.
The file descriptor.
Drives the async I/O scheduler.
Call this once per frame, before task.step, to resume any coroutines that are waiting on socket I/O (recv, send, accept, connect).
while running do
_socket.poll()
task.step()
end
How long to wait for I/O readiness, in seconds. 0 (default) returns immediately if nothing is ready. -1 blocks until at least one fd is ready.
Number of coroutines that were resumed this call.
Creates a new socket.
local tcp = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
local udp = _socket.socket(_socket.AF_INET, _socket.SOCK_DGRAM)
Address family - AF_INET (IPv4) or AF_INET6 (IPv6).
Socket type - SOCK_STREAM (TCP) or SOCK_DGRAM (UDP).
Protocol number, or nil to auto-select (usually 0).
The newly created socket.
Resolves a host/service pair into a list of address records suitable for creating and connecting sockets.
local results = _socket.getaddrinfo("example.com", 443, _socket.AF_INET, _socket.SOCK_STREAM)
for _, info in results do
print(info.addr, info.port)
end
Hostname or numeric address, or nil for the wildcard address.
Service name (e.g. "http") or port number.
Desired address family, or AF_UNSPEC to allow any.
Desired socket type (e.g. SOCK_STREAM), or nil for any.
Desired protocol, or nil for any.
Bitwise OR of AI_* flags (e.g. AI_PASSIVE).
An array of resolved address records.
Reverse-resolves a numeric address and port into a hostname and service name.
The numeric IP address to look up.
The port number.
Bitwise OR of NI_* flags to control name resolution.
The resolved hostname.
The resolved service name.
Converts a 16-bit value from host byte order to network byte order.
Converts a 16-bit value from network byte order to host byte order.
Converts a 32-bit value from host byte order to network byte order.
Converts a 32-bit value from network byte order to host byte order.
Result record from getAddrInfo, representing a single resolved address with its protocol metadata.