Module socket.inet
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) | Constructor for inet socket objects. |
inet:receive (...) | Receives data from the socket. |
inet:send (msg, addr, port) | Sends data through the socket. |
Class inet
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.,
socket.sock.STREAM
). - proto
The protocol (e.g.,
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:
-
(table or nil) A new socket object for the accepted connection, or nil and an error message.
See 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.
Returns:
-
(boolean or nil) True on success, or nil and an error message on failure.
See 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.,
socket.sock.DGRAM
). - proto
The protocol (e.g.,
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 inet:receive that converts the raw IP address
from net.aton format to a string using net.ntoa.
Parameters:
- len (number) [optional] The maximum number of bytes to receive.
- flags (number) [optional] Flags for the receive operation.
Returns:
- (string or nil) The received data, or nil on error.
- (string or nil) The sender's IP address, or an error message.
- (number or nil) The sender's port number.
See 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:
-
(boolean or nil) True on success, or nil and an error message on failure.
See 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.
Returns:
-
(boolean or nil) True on success, or nil and an error message on failure.
See 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 or nil) True on success, or nil and an error message on failure.
See 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)
-
Constructor for inet socket objects.
Initializes a new socket object, setting up its metatable for OOP-like behavior.
Parameters:
- o (table) [optional] An initial table to be used as the object.
Returns:
-
(table) The new inet socket object.
- inet:receive (...)
-
Receives data from the socket.
For UDP, if the
raw
parameter (third boolean) is true, it returns the raw IP address.Parameters:
- ...
Varargs passed directly to the underlying
socket:receive()
. Typically(len, flags, raw_ip_for_udp)
.
Returns:
-
Varargs returned by the underlying
socket:receive()
. Typically(data, ip_address, port)
or(data, error_message)
.See also:
- ...
Varargs passed directly to the underlying
- inet:send (msg, addr, port)
-
Sends data through the socket.
If
addr
andport
are 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:
-
(boolean or nil) True on success, or nil and an error message on failure.
See also: