socket.inetModule
Internet (AF_INET) socket operations.
This module provides a higher-level abstraction over the raw socket and net modules for creating and managing INET (IPv4) sockets, including TCP and UDP.It simplifies common socket operations like binding, connecting, sending, and receiving data.
See also
Class inet
| inet.tcp | TCP socket specialization. |
| inet.tcp:accept (flags) | Accepts an incoming connection on a listening TCP socket. |
| inet.tcp:listen (backlog) | Listens for incoming connections on a TCP socket. |
| inet.udp | UDP socket specialization. |
| inet.udp:receivefrom (len, flags) | Receives data from a UDP socket, along with the sender’s address. |
| inet.udp:sendto (msg, addr, port) | Alias for inet.udp:send. |
| inet:__call () | Metamethod to create a new socket instance when inet() or inet.tcp() or inet.udp() is called. |
| inet:bind (addr, port) | Binds the socket to a specific address and port. |
| inet:close () | Closes the underlying socket. |
| inet:connect (addr, port, flags) | Connects the socket to a remote address and port. |
| inet:getpeername () | Gets the remote address (IP and port) to which the socket is connected. |
| inet:getsockname () | Gets the local address (IP and port) to which the socket is bound. |
| inet:new ([o]) | Derives a specialization of the class, as inet.tcp and inet.udp are, or wraps a table in it. |
| inet:receive (...) | Receives data from the socket; inet.udp:receivefrom also returns the sender. |
| inet:send (msg, addr, port) | Sends data through the socket. |
Class inet in detail
Base class for socket types.
- inet.tcp
-
TCP socket specialization.
Provides methods specific to TCP sockets (e.g., listen, accept).
Create TCP sockets using
inet.tcp().Fields
- type
The socket type (e.g.,
linux.socket.sock.STREAM). - proto
The protocol (e.g.,
linux.socket.ipproto.TCP).
- type
The socket type (e.g.,
- inet.tcp:accept (flags)
-
Accepts an incoming connection on a listening TCP socket.
Parameters
- flags (number) [optional] Flags for the accept operation.
Returns
-
(socket) The accepted connection, as a plain socket object, or
niland"EAGAIN"when the wait ended with no connection to accept.Raises
error on failureSee also
- inet.tcp:listen (backlog)
-
Listens for incoming connections on a TCP socket.
Parameters
- backlog (number) The maximum length of the queue of pending connections.
Raises
error on failureSee also
- inet.udp
-
UDP socket specialization.
Provides methods specific to UDP sockets (e.g., receivefrom, sendto).
Create UDP sockets using
inet.udp().Fields
- type
The socket type (e.g.,
linux.socket.sock.DGRAM). - proto
The protocol (e.g.,
linux.socket.ipproto.UDP).
- type
The socket type (e.g.,
- inet.udp:receivefrom (len, flags)
-
Receives data from a UDP socket, along with the sender’s address.
This is a wrapper around socket:receivefrom that converts the raw IP address
from net.aton format to a string using net.ntoa.
Parameters
- len (number) The maximum number of bytes to receive.
- flags (number) [optional] Flags for the receive operation.
Returns
-
(string) The received data, or
nilwhen the wait ended with nothing to read. -
(string) The sender’s IP address, or
"EAGAIN"beside thatnil. - (number) The sender’s port number.
Raises
error on failureSee also
- inet.udp:sendto (msg, addr, port)
-
Alias for
inet.udp:send. Sends data to a specific address and port using UDP.Parameters
- msg (string) The message to send.
- addr (string) The destination IP address.
- port (number) The destination port.
Returns
-
(number|boolean) Number of bytes sent, or
falsewhen a send timeout ended the wait with nothing queued.Raises
error on failureSee also
- inet:__call ()
-
Metamethod to create a new socket instance when
inet()orinet.tcp()orinet.udp()is called. This is the primary way to create new socket instances (e.g.,local sock = inet.tcp()).Returns
-
(table) A new socket object (e.g., a TCP or UDP socket object).
See also
Usage
local tcp_socket = inet.tcp() local udp_socket = inet.udp()
- inet:bind (addr, port)
-
Binds the socket to a specific address and port.
The address ‘*’ is treated as
INADDR_ANY(0.0.0.0).Parameters
- addr (string) The IP address to bind to. Use ‘*’ for any address.
- port (number) The port number to bind to.
Raises
error on failureSee also
- inet:close ()
-
Closes the underlying socket.
See also
- inet:connect (addr, port, flags)
-
Connects the socket to a remote address and port.
Parameters
- addr (string) The remote IP address.
- port (number) The remote port.
- flags (number) [optional] Connection flags.
Returns
-
(boolean)
trueonce connected, orniland"EINPROGRESS"when the connection it started is still in progress,"EALREADY"when an earlier connect started it.Raises
error on failureSee also
- inet:getpeername ()
-
Gets the remote address (IP and port) to which the socket is connected.
Returns
- (string) Remote IP address.
- (number) Remote port number.
See also
- inet:getsockname ()
-
Gets the local address (IP and port) to which the socket is bound.
Returns
- (string) Local IP address.
- (number) Local port number.
See also
- inet:new ([o])
-
Derives a specialization of the class, as inet.tcp and inet.udp are, or wraps a table in
it. It opens no socket: calling the class does,
inet.tcp().Parameters
- o table an initial object table. (optional)
Returns
-
inet
the derived class or the wrapped table.
See also
- inet:receive (...)
-
Receives data from the socket; inet.udp:receivefrom also returns the sender.
Parameters
- ...
Varargs passed directly to the underlying
socket:receive():(len[, flags]).
Returns
-
The data the underlying
socket:receive()returns.Raises
error on failureSee also
- ...
Varargs passed directly to the underlying
- inet:send (msg, addr, port)
-
Sends data through the socket.
If
addrandportare provided (typical for UDP), the address is converted using net.aton before sending.Parameters
- msg (string) The message to send.
- addr (string) [optional] The destination IP address (e.g., for UDP).
- port (number) [optional] The destination port (e.g., for UDP).
Returns
-
(number|boolean) Number of bytes sent, or
falsewhen a send timeout ended the wait with nothing queued.Raises
error on failureSee also