Module socket.unix
UNIX domain socket (AF_UNIX) operations.
This module provides an OOP-style abstraction over the socket module for AF_UNIX sockets, modeled after socket.inet.
A socket instance can be created with an optional path stored as default
address, which is then reused automatically in bind, connect, and
dgram:sendto without an explicit per-call address.
See also:
Class unix
| unix.dgram | DGRAM socket specialization (connectionless). |
| unix.dgram:receivefrom (len, flags) | Receives data from a DGRAM socket along with the sender's path. |
| unix.dgram:sendto (msg, path) | Sends data to a UNIX domain socket path. |
| unix.stream | STREAM socket specialization (connection-oriented). |
| unix.stream:accept (flags) | Accepts an incoming connection on a listening STREAM socket. |
| unix.stream:listen (backlog) | Listens for incoming connections on a STREAM socket. |
| unix:__call (path) | Creates a new UNIX domain socket instance. |
| unix:bind (path) | Binds the socket to a UNIX domain path. |
| unix:close () | Closes the underlying socket. |
| unix:connect (path) | Connects the socket to a UNIX domain path. |
| unix:getpeername () | Gets the remote address (path) the socket is connected to. |
| unix:getsockname () | Gets the local address (path) of the socket. |
| unix:new (o) | Constructor for unix socket objects. |
| unix:receive (...) | Receives data from the socket. |
| unix:send (msg, path) | Sends data through the socket. |
Class unix
Base class for UNIX domain socket objects.
- unix.dgram
-
DGRAM socket specialization (connectionless).
Create instances with
unix.dgram([path]). The optional path is stored as the default destination for sendto.Fields:
- type
The socket type (
socket.sock.DGRAM).
- type
The socket type (
- unix.dgram:receivefrom (len, flags)
-
Receives data from a DGRAM socket along with the sender's path.
Parameters:
- len (number) [optional] Maximum number of bytes to receive.
- flags (number) [optional] Receive flags.
Returns:
- (string or nil) The received data.
- (string or nil) The sender's socket path.
See also:
- unix.dgram:sendto (msg, path)
-
Sends data to a UNIX domain socket path.
If
pathis omitted, uses the path provided at construction time.Parameters:
- msg (string) The message to send.
- path (string) [optional] Destination socket path.
Returns:
-
(number) Number of bytes sent on success.
Raises:
error on failure.See also:
- unix.stream
-
STREAM socket specialization (connection-oriented).
Create instances with
unix.stream([path]).Fields:
- type
The socket type (
socket.sock.STREAM).
- type
The socket type (
- unix.stream:accept (flags)
-
Accepts an incoming connection on a listening STREAM socket.
Parameters:
- flags (number) [optional] Flags for the accept operation.
Returns:
-
A new socket object for the accepted connection.
See also:
- unix.stream:listen (backlog)
-
Listens for incoming connections on a STREAM socket.
Parameters:
- backlog (number) Maximum length of the queue of pending connections.
See also:
- unix:__call (path)
-
Creates a new UNIX domain socket instance.
This is the primary way to create instances (e.g.
local s = unix.stream(path)).Parameters:
- path (string) [optional] Default UNIX socket path stored in the object. Reused automatically by bind, connect, send, sendto, and receivefrom when no explicit path is given.
Returns:
-
(table) A new unix socket object.
See also:
- unix:bind (path)
-
Binds the socket to a UNIX domain path.
Parameters:
- path (string) [optional] Path to bind to. Defaults to the path provided at construction time.
See also:
- unix:close ()
-
Closes the underlying socket.
See also:
- unix:connect (path)
-
Connects the socket to a UNIX domain path.
Parameters:
- path (string) [optional] Path to connect to. Defaults to the path provided at construction time.
See also:
- unix:getpeername ()
-
Gets the remote address (path) the socket is connected to.
Returns:
-
(string) The remote socket path.
See also:
- unix:getsockname ()
-
Gets the local address (path) of the socket.
Returns:
-
(string) The local socket path.
See also:
- unix:new (o)
-
Constructor for unix socket objects.
Initializes a new socket object and sets up its metatable.
Parameters:
- o (table) [optional] An initial table to use as the object.
Returns:
-
(table) The new unix socket object.
- unix:receive (...)
-
Receives data from the socket.
Parameters:
- ...
Varargs passed directly to
socket:receive().
Returns:
-
Varargs returned by
socket:receive().See also:
- ...
Varargs passed directly to
- unix:send (msg, path)
-
Sends data through the socket.
For connected STREAM sockets,
pathis omitted. For DGRAM sockets, usesendto()to send to the stored path or an explicit destination.Parameters:
- msg (string) The message to send.
- path (string) [optional] Explicit destination socket path.
Returns:
-
(number) Number of bytes sent on success.
Raises:
error on failure.See also: