A blocking TCP client wrapper.
All read and write operations suspend the current coroutine until the operation completes or the configured timeout is hit.
@eryx/net ModuleHigh-level TCP and UDP networking helpers built on top of @eryx/_socket.
This module wraps the low-level BSD-style socket API with friendlier client/server classes, socket-option helpers, IPv4/IPv6 family selection, and both signal-driven and manual server accept loops.
local net = require("@eryx/net")
local client = net.TcpClient.new("127.0.0.1", 8080, {
nodelay = true,
timeout = 5,
})
client:write("Hello, server!")
local response = client:read(1024)
print(buffer.tostring(response))
client:close()
local net = require("@eryx/net")
local server = net.TcpServer.new("0.0.0.0", 8080, {
reuseAddress = true,
handler = function(client)
client:write("hello\\n")
client:close()
end,
})
local net = require("@eryx/net")
local server = net.UdpServer.new("127.0.0.1", 9000)
local client = net.UdpClient.new("127.0.0.1", 9000)
client:write("ping")
local data, host, port = server:receive(1024)
server:writeTo("pong", host, port)
print(buffer.tostring(data))
client:close()
server:close()
A blocking TCP client wrapper.
All read and write operations suspend the current coroutine until the operation completes or the configured timeout is hit.
Underlying connected socket.
Remote host this client is connected to.
Remote port this client is connected to.
Address family in use, such as AF_INET or AF_INET6.
Creates and connects a blocking TCP client.
Remote hostname or numeric address.
Remote TCP port.
Optional socket configuration.
A connected blocking TCP client.
Wraps an already-connected socket as a TcpClient.
This is primarily used internally by TcpServer.accept, but can also be useful for advanced code that obtains connected sockets elsewhere.
Already-connected TCP socket.
Remote peer address.
Remote peer port.
Address family override.
Optional wrapper configuration to apply.
Wrapped blocking TCP client.
Sends the provided data to the remote peer.
Payload to send.
Reads up to bytes bytes from the remote peer.
Maximum number of bytes to receive.
Received data.
Updates the blocking timeout for future operations on this client.
Passing nil restores blocking mode with no timeout.
Timeout in seconds, or nil for no timeout.
Sets a socket option on the underlying connection.
Protocol level such as SOL_SOCKET.
Option constant such as SO_KEEPALIVE.
Value to set.
Reads a socket option from the underlying connection.
Protocol level such as SOL_SOCKET.
Option constant to query.
Current option value.
Returns the remote address and port of this connection.
Remote host.
Remote port.
Returns the local address and port used by this connection.
Local host.
Local port.
Shuts down one or both halves of the TCP connection.
One of _socket.SHUT_RD, _socket.SHUT_WR, or _socket.SHUT_RDWR.
Closes the TCP connection.
A non-blocking TCP client wrapper.
Read operations return nil when no data is available yet, and write
operations return false when the socket would block.
Underlying connected socket.
Remote host this client is connected to.
Remote port this client is connected to.
Address family in use, such as AF_INET or AF_INET6.
Creates and connects a non-blocking TCP client.
Remote hostname or numeric address.
Remote TCP port.
Optional socket configuration.
A connected non-blocking TCP client.
Wraps an already-connected socket as a TcpClientNonBlocking.
Already-connected TCP socket.
Remote peer address.
Remote peer port.
Address family override.
Optional wrapper configuration to apply.
Wrapped non-blocking TCP client.
Attempts to send the full payload without blocking.
Payload to send.
true when the payload was fully sent, false when the socket would block.
Attempts to read up to bytes bytes without blocking.
Maximum number of bytes to receive.
Received data, or nil if the socket would block.
Sets a socket option on the underlying connection.
Protocol level such as SOL_SOCKET.
Option constant such as SO_KEEPALIVE.
Value to set.
Reads a socket option from the underlying connection.
Protocol level such as SOL_SOCKET.
Option constant to query.
Current option value.
Returns the remote address and port of this connection.
Remote host.
Remote port.
Returns the local address and port used by this connection.
Local host.
Local port.
Shuts down one or both halves of the TCP connection.
One of _socket.SHUT_RD, _socket.SHUT_WR, or _socket.SHUT_RDWR.
Closes the TCP connection.
A TCP server wrapper.
In signal mode, a background accept loop dispatches each connection
through TcpServer.onClient. In manual mode, callers explicitly use
TcpServer.accept or TcpServer.acceptNonBlocking.
Underlying listening socket.
Fired when the background accept loop encounters an error.
Bound local host.
Bound local port.
Address family in use, such as AF_INET or AF_INET6.
Creates and starts a TCP server.
When options.mode is "signal" or omitted, the server immediately starts
a background accept loop. When set to "manual", no background loop is
started and callers should use TcpServer.accept themselves.
Local address to bind to.
Local port to listen on. Use 0 to let the OS choose.
Optional listener configuration.
A listening TCP server.
Accepts the next incoming connection as a blocking TcpClient.
In manual mode this is typically called by user code. In signal mode it
is used internally by the background accept loop.
Accepted client, or nil when the listening socket is non-blocking and no client is ready.
Accepts the next incoming connection as a non-blocking TcpClientNonBlocking.
Accepted client, or nil when the listening socket is non-blocking and no client is ready.
Registers a handler on TcpServer.onClient.
Callback to run for each accepted client.
Returns the bound local address and port of the listening socket.
Local host.
Local port.
Stops accepting new connections and closes the listening socket.
A UDP client wrapper.
Unlike TCP, UDP is datagram-oriented: each send corresponds to one packet and reads may return complete datagrams from a connected peer.
Underlying UDP socket.
Default remote host.
Default remote port.
Address family in use, such as AF_INET or AF_INET6.
Creates a UDP socket and connects it to a default remote peer.
Connected UDP sockets can still use UdpClient.writeTo to override the destination per datagram.
Default remote hostname or numeric address.
Default remote UDP port.
Optional socket configuration.
Connected UDP client wrapper.
Sends one datagram to the connected peer.
Datagram payload.
Number of bytes written, or nil if the socket would block.
Sends one datagram to an explicit destination.
Datagram payload.
Destination host.
Destination port.
Number of bytes written, or nil if the socket would block.
Receives one datagram payload from the connected peer.
Maximum datagram size to receive.
Datagram payload, or nil if the socket would block.
Receives one datagram along with its sender address.
Maximum datagram size to receive.
Datagram payload.
Sender host.
Sender port.
Updates the blocking timeout for future operations on this client.
Passing nil restores blocking mode with no timeout.
Timeout in seconds, or nil for no timeout.
Sets a socket option on the underlying UDP socket.
Protocol level such as SOL_SOCKET.
Option constant such as SO_BROADCAST.
Value to set.
Reads a socket option from the underlying UDP socket.
Protocol level such as SOL_SOCKET.
Option constant to query.
Current option value.
Returns the default remote peer for this connected UDP socket.
Remote host.
Remote port.
Returns the local address and port used by this UDP socket.
Local host.
Local port.
Closes the UDP socket.
A UDP server wrapper.
In signal mode, a background receive loop dispatches datagrams through
UdpServer.onDatagram. In manual mode, callers explicitly use
UdpServer.receive.
Underlying bound UDP socket.
Fired with (data, host, port) for each received datagram in signal mode.
Fired when the background receive loop encounters an error.
Bound local host.
Bound local port.
Address family in use, such as AF_INET or AF_INET6.
Creates and binds a UDP server socket.
When options.mode is "signal", the server immediately starts a
background receive loop that fires UdpServer.onDatagram.
Local address to bind to.
Local UDP port. Use 0 to let the OS choose.
Optional socket configuration.
Bound UDP server wrapper.
Receives one datagram and its sender address.
Maximum datagram size to receive.
Datagram payload.
Sender host.
Sender port.
Sends one datagram to the specified destination.
Datagram payload.
Destination host.
Destination port.
Number of bytes written, or nil if the socket would block.
Updates the blocking timeout for future operations on this server socket.
Passing nil restores blocking mode with no timeout.
Timeout in seconds, or nil for no timeout.
Sets a socket option on the underlying UDP socket.
Protocol level such as SOL_SOCKET.
Option constant such as SO_BROADCAST.
Value to set.
Reads a socket option from the underlying UDP socket.
Protocol level such as SOL_SOCKET.
Option constant to query.
Current option value.
Returns the local address and port of the bound UDP socket.
Local host.
Local port.
Stops the background receive loop, if any, and closes the UDP socket.
A single socket option assignment used by the higher-level net wrappers.
Each entry maps directly to one setsockopt call on the underlying socket.
Protocol level such as SOL_SOCKET or IPPROTO_TCP.
Option constant such as SO_REUSEADDR or TCP_NODELAY.
Value to assign to the option.
Common socket configuration shared by TCP and UDP clients and servers.
All fields are optional. When omitted, the wrapper chooses sensible defaults for the socket kind being created.
Address family, typically AF_INET or AF_INET6.
Blocking timeout in seconds. Ignored in non-blocking mode.
Whether the wrapper should operate in blocking mode. Defaults to true.
Extra raw socket options to apply after the socket is created.
Enables SO_REUSEADDR.
Enables SO_KEEPALIVE.
Enables TCP_NODELAY for TCP sockets.
Enables SO_BROADCAST for UDP broadcast traffic.
Sets SO_RCVBUF.
Sets SO_SNDBUF.
Sets IPV6_V6ONLY on IPv6 sockets.
Optional local address to bind before connect.
Optional local port to bind before connect.
Options for TcpClient.new and TcpClientNonBlocking.new.
Address family, typically AF_INET or AF_INET6.
Blocking timeout in seconds. Ignored in non-blocking mode.
Whether the wrapper should operate in blocking mode. Defaults to true.
Extra raw socket options to apply after the socket is created.
Enables SO_REUSEADDR.
Enables SO_KEEPALIVE.
Enables TCP_NODELAY for TCP sockets.
Enables SO_BROADCAST for UDP broadcast traffic.
Sets SO_RCVBUF.
Sets SO_SNDBUF.
Sets IPV6_V6ONLY on IPv6 sockets.
Optional local address to bind before connect.
Optional local port to bind before connect.
Options for TcpServer.new.
Maximum queued connection count passed to listen.
signal starts a background accept loop; manual leaves accepting to the caller.
Optional convenience handler connected to onClient.
Address family, typically AF_INET or AF_INET6.
Blocking timeout in seconds. Ignored in non-blocking mode.
Whether the wrapper should operate in blocking mode. Defaults to true.
Extra raw socket options to apply after the socket is created.
Enables SO_REUSEADDR.
Enables SO_KEEPALIVE.
Enables TCP_NODELAY for TCP sockets.
Enables SO_BROADCAST for UDP broadcast traffic.
Sets SO_RCVBUF.
Sets SO_SNDBUF.
Sets IPV6_V6ONLY on IPv6 sockets.
Optional local address to bind before connect.
Optional local port to bind before connect.
Options for UdpClient.new.
Address family, typically AF_INET or AF_INET6.
Blocking timeout in seconds. Ignored in non-blocking mode.
Whether the wrapper should operate in blocking mode. Defaults to true.
Extra raw socket options to apply after the socket is created.
Enables SO_REUSEADDR.
Enables SO_KEEPALIVE.
Enables TCP_NODELAY for TCP sockets.
Enables SO_BROADCAST for UDP broadcast traffic.
Sets SO_RCVBUF.
Sets SO_SNDBUF.
Sets IPV6_V6ONLY on IPv6 sockets.
Optional local address to bind before connect.
Optional local port to bind before connect.
Options for UdpServer.new.
signal starts a background receive loop; manual leaves receiving to the caller.
Optional convenience handler connected to onDatagram.
Datagram buffer size used by the background receive loop.
Address family, typically AF_INET or AF_INET6.
Blocking timeout in seconds. Ignored in non-blocking mode.
Whether the wrapper should operate in blocking mode. Defaults to true.
Extra raw socket options to apply after the socket is created.
Enables SO_REUSEADDR.
Enables SO_KEEPALIVE.
Enables TCP_NODELAY for TCP sockets.
Enables SO_BROADCAST for UDP broadcast traffic.
Sets SO_RCVBUF.
Sets SO_SNDBUF.
Sets IPV6_V6ONLY on IPv6 sockets.
Optional local address to bind before connect.
Optional local port to bind before connect.