|
|
@@ -11,22 +11,31 @@ To use this module, you need to import it first: `import("core.base.socket")`
|
|
|
|
|
|
- Create a TCP socket
|
|
|
|
|
|
-```lua
|
|
|
-import("core.base.socket")
|
|
|
+#### Function Prototype
|
|
|
|
|
|
-local sock = socket.tcp()
|
|
|
+::: tip API
|
|
|
+```lua
|
|
|
+socket.tcp(opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
+
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| opt | Optional. Option parameters |
|
|
|
+
|
|
|
+Options:
|
|
|
+- `family`: Address family, options:
|
|
|
+ - `socket.IPV4` (1) - IPv4 address family (default)
|
|
|
+ - `socket.IPV6` (2) - IPv6 address family
|
|
|
+
|
|
|
+#### Usage
|
|
|
|
|
|
Creates a TCP socket object (`socket.TCP`), using IPv4 by default.
|
|
|
|
|
|
TCP is a connection-oriented, reliable stream protocol that guarantees data arrives in order, suitable for most network communication scenarios.
|
|
|
|
|
|
-Parameters:
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `family`: Address family, options:
|
|
|
- - `socket.IPV4` (1) - IPv4 address family (default)
|
|
|
- - `socket.IPV6` (2) - IPv6 address family
|
|
|
-
|
|
|
```lua
|
|
|
-- Create IPv4 TCP socket
|
|
|
local sock = socket.tcp()
|
|
|
@@ -39,20 +48,29 @@ local sock = socket.tcp({family = socket.IPV6})
|
|
|
|
|
|
- Create a UDP socket
|
|
|
|
|
|
-```lua
|
|
|
-import("core.base.socket")
|
|
|
+#### Function Prototype
|
|
|
|
|
|
-local sock = socket.udp()
|
|
|
+::: tip API
|
|
|
+```lua
|
|
|
+socket.udp(opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
+
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| opt | Optional. Option parameters |
|
|
|
+
|
|
|
+Options:
|
|
|
+- `family`: Address family, can be `socket.IPV4` (default) or `socket.IPV6`
|
|
|
+
|
|
|
+#### Usage
|
|
|
|
|
|
Creates a UDP socket object (`socket.UDP`) for connectionless datagram communication.
|
|
|
|
|
|
UDP is a connectionless, unreliable datagram protocol that doesn't guarantee data arrival or order, but has low latency, suitable for real-time communication, broadcasting, etc.
|
|
|
|
|
|
-Parameters:
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `family`: Address family, can be `socket.IPV4` (default) or `socket.IPV6`
|
|
|
-
|
|
|
UDP is suitable for scenarios requiring low latency and can tolerate some packet loss:
|
|
|
|
|
|
```lua
|
|
|
@@ -75,11 +93,19 @@ sock:close()
|
|
|
|
|
|
- Create a Unix domain socket
|
|
|
|
|
|
-```lua
|
|
|
-import("core.base.socket")
|
|
|
+#### Function Prototype
|
|
|
|
|
|
-local sock = socket.unix()
|
|
|
+::: tip API
|
|
|
+```lua
|
|
|
+socket.unix()
|
|
|
```
|
|
|
+:::
|
|
|
+
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+No parameters required for this function.
|
|
|
+
|
|
|
+#### Usage
|
|
|
|
|
|
Creates a Unix domain socket (address family `socket.UNIX`) for inter-process communication on the same machine.
|
|
|
|
|
|
@@ -91,18 +117,25 @@ Only available on Unix/Linux/macOS systems. Suitable for high-performance local
|
|
|
|
|
|
- Create and bind a TCP socket
|
|
|
|
|
|
-```lua
|
|
|
-import("core.base.socket")
|
|
|
+#### Function Prototype
|
|
|
|
|
|
-local sock = socket.bind(addr, port, opt)
|
|
|
+::: tip API
|
|
|
+```lua
|
|
|
+socket.bind(addr: <string>, port: <number>, opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Creates a TCP socket and binds it to the specified address and port, typically used for servers.
|
|
|
+#### Parameter Description
|
|
|
|
|
|
-Parameters:
|
|
|
-- `addr`: IP address, such as `"127.0.0.1"` or `"0.0.0.0"`
|
|
|
-- `port`: Port number
|
|
|
-- `opt` (optional): Option parameters, same as [socket.tcp](#socket-tcp)
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| addr | Required. IP address, such as "127.0.0.1" or "0.0.0.0" |
|
|
|
+| port | Required. Port number |
|
|
|
+| opt | Optional. Option parameters, same as socket.tcp |
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Creates a TCP socket and binds it to the specified address and port, typically used for servers.
|
|
|
|
|
|
Complete TCP echo server example:
|
|
|
|
|
|
@@ -145,18 +178,27 @@ end
|
|
|
|
|
|
- Create and bind a Unix domain socket
|
|
|
|
|
|
-```lua
|
|
|
-import("core.base.socket")
|
|
|
+#### Function Prototype
|
|
|
|
|
|
-local sock = socket.bind_unix(addr, opt)
|
|
|
+::: tip API
|
|
|
+```lua
|
|
|
+socket.bind_unix(addr: <string>, opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Creates a Unix domain socket and binds it to the specified path.
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| addr | Required. Unix domain socket path |
|
|
|
+| opt | Optional. Option parameters |
|
|
|
|
|
|
-Parameters:
|
|
|
-- `addr`: Unix domain socket path
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `is_abstract`: Whether to use abstract namespace (Linux only)
|
|
|
+Options:
|
|
|
+- `is_abstract`: Whether to use abstract namespace (Linux only)
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Creates a Unix domain socket and binds it to the specified path.
|
|
|
|
|
|
```lua
|
|
|
import("core.base.socket")
|
|
|
@@ -170,20 +212,29 @@ server:listen(10)
|
|
|
|
|
|
- Create and connect a TCP socket
|
|
|
|
|
|
-```lua
|
|
|
-import("core.base.socket")
|
|
|
+#### Function Prototype
|
|
|
|
|
|
-local sock = socket.connect(addr, port, opt)
|
|
|
+::: tip API
|
|
|
+```lua
|
|
|
+socket.connect(addr: <string>, port: <number>, opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Creates a TCP socket and connects to the specified address and port, used for clients.
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| addr | Required. Server IP address |
|
|
|
+| port | Required. Server port number |
|
|
|
+| opt | Optional. Option parameters |
|
|
|
+
|
|
|
+Options:
|
|
|
+- `family`: Address family
|
|
|
+- `timeout`: Connection timeout (milliseconds)
|
|
|
|
|
|
-Parameters:
|
|
|
-- `addr`: Server IP address
|
|
|
-- `port`: Server port number
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `family`: Address family
|
|
|
- - `timeout`: Connection timeout (milliseconds)
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Creates a TCP socket and connects to the specified address and port, used for clients.
|
|
|
|
|
|
Complete TCP client example:
|
|
|
|
|
|
@@ -220,27 +271,49 @@ end
|
|
|
|
|
|
- Create and connect a Unix domain socket
|
|
|
|
|
|
-```lua
|
|
|
-import("core.base.socket")
|
|
|
+#### Function Prototype
|
|
|
|
|
|
-local sock = socket.connect_unix(addr, opt)
|
|
|
+::: tip API
|
|
|
+```lua
|
|
|
+socket.connect_unix(addr: <string>, opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Creates a Unix domain socket and connects to the specified path.
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| addr | Required. Unix domain socket path |
|
|
|
+| opt | Optional. Option parameters |
|
|
|
|
|
|
-Parameters:
|
|
|
-- `addr`: Unix domain socket path
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `is_abstract`: Whether to use abstract namespace (Linux only)
|
|
|
- - `timeout`: Connection timeout
|
|
|
+Options:
|
|
|
+- `is_abstract`: Whether to use abstract namespace (Linux only)
|
|
|
+- `timeout`: Connection timeout
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Creates a Unix domain socket and connects to the specified path.
|
|
|
|
|
|
## socket:bind
|
|
|
|
|
|
- Bind socket to address
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local ok = sock:bind(addr, port)
|
|
|
+socket:bind(addr: <string>, port: <number>)
|
|
|
```
|
|
|
+:::
|
|
|
+
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| addr | Required. IP address |
|
|
|
+| port | Required. Port number |
|
|
|
+
|
|
|
+#### Usage
|
|
|
|
|
|
Binds the socket to the specified IP address and port.
|
|
|
|
|
|
@@ -250,14 +323,23 @@ Return value: Returns a positive number on success, -1 and error message on fail
|
|
|
|
|
|
- Start listening for connections
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local ok = sock:listen(backlog)
|
|
|
+socket:listen(backlog: <number>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Makes the socket start listening for client connections, used for servers.
|
|
|
+#### Parameter Description
|
|
|
|
|
|
-Parameters:
|
|
|
-- `backlog`: Maximum length of the pending connection queue, default 10
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| backlog | Optional. Maximum length of the pending connection queue, default 10 |
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Makes the socket start listening for client connections, used for servers.
|
|
|
|
|
|
Must be called after `bind` and before `accept`.
|
|
|
|
|
|
@@ -265,15 +347,26 @@ Must be called after `bind` and before `accept`.
|
|
|
|
|
|
- Accept client connection
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local client_sock = sock:accept(opt)
|
|
|
+socket:accept(opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Accepts a client connection, returns a new socket object for communicating with the client.
|
|
|
+#### Parameter Description
|
|
|
|
|
|
-Parameters:
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `timeout`: Timeout (milliseconds), default -1 (infinite wait)
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| opt | Optional. Option parameters |
|
|
|
+
|
|
|
+Options:
|
|
|
+- `timeout`: Timeout (milliseconds), default -1 (infinite wait)
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Accepts a client connection, returns a new socket object for communicating with the client.
|
|
|
|
|
|
Return value: Returns client socket object on success, nil and error message on failure.
|
|
|
|
|
|
@@ -291,17 +384,28 @@ end
|
|
|
|
|
|
- Connect to remote address
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local ok = sock:connect(addr, port, opt)
|
|
|
+socket:connect(addr: <string>, port: <number>, opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Connects to the specified remote address and port.
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| addr | Required. Target IP address |
|
|
|
+| port | Required. Target port number |
|
|
|
+| opt | Optional. Option parameters |
|
|
|
|
|
|
-Parameters:
|
|
|
-- `addr`: Target IP address
|
|
|
-- `port`: Target port number
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `timeout`: Connection timeout (milliseconds)
|
|
|
+Options:
|
|
|
+- `timeout`: Connection timeout (milliseconds)
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Connects to the specified remote address and port.
|
|
|
|
|
|
Return value: Returns a positive number on success, -1 and error message on failure.
|
|
|
|
|
|
@@ -309,18 +413,29 @@ Return value: Returns a positive number on success, -1 and error message on fail
|
|
|
|
|
|
- Send data
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local sent = sock:send(data, opt)
|
|
|
+socket:send(data: <string|bytes>, opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Sends data through the socket.
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| data | Required. Data to send, can be string or bytes object |
|
|
|
+| opt | Optional. Option parameters |
|
|
|
|
|
|
-Parameters:
|
|
|
-- `data`: Data to send, can be string or bytes object
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `block`: Whether to block sending, default false
|
|
|
- - `start`: Data start position, default 1
|
|
|
- - `last`: Data end position, default is data size
|
|
|
+Options:
|
|
|
+- `block`: Whether to block sending, default false
|
|
|
+- `start`: Data start position, default 1
|
|
|
+- `last`: Data end position, default is data size
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Sends data through the socket.
|
|
|
|
|
|
Return value: Actual number of bytes sent, returns -1 on failure.
|
|
|
|
|
|
@@ -341,18 +456,29 @@ end
|
|
|
|
|
|
- Receive data
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local recv, data = sock:recv(buff, size, opt)
|
|
|
+socket:recv(buff: <bytes>, size: <number>, opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Receives data from the socket.
|
|
|
+#### Parameter Description
|
|
|
|
|
|
-Parameters:
|
|
|
-- `buff`: bytes buffer object
|
|
|
-- `size`: Number of bytes to receive
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `block`: Whether to block receiving, default false
|
|
|
- - `timeout`: Timeout (milliseconds)
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| buff | Required. bytes buffer object |
|
|
|
+| size | Required. Number of bytes to receive |
|
|
|
+| opt | Optional. Option parameters |
|
|
|
+
|
|
|
+Options:
|
|
|
+- `block`: Whether to block receiving, default false
|
|
|
+- `timeout`: Timeout (milliseconds)
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Receives data from the socket.
|
|
|
|
|
|
Return values:
|
|
|
- `recv`: Actual number of bytes received, returns -1 on failure
|
|
|
@@ -377,17 +503,26 @@ end
|
|
|
|
|
|
- Send datagram (UDP)
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local sent = sock:sendto(data, addr, port, opt)
|
|
|
+socket:sendto(data: <string|bytes>, addr: <string>, port: <number>, opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Sends a datagram to the specified address via UDP socket.
|
|
|
+#### Parameter Description
|
|
|
|
|
|
-Parameters:
|
|
|
-- `data`: Data to send, can be string or bytes object
|
|
|
-- `addr`: Target IP address
|
|
|
-- `port`: Target port number
|
|
|
-- `opt` (optional): Option parameters
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| data | Required. Data to send, can be string or bytes object |
|
|
|
+| addr | Required. Target IP address |
|
|
|
+| port | Required. Target port number |
|
|
|
+| opt | Optional. Option parameters |
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Sends a datagram to the specified address via UDP socket.
|
|
|
|
|
|
Return value: Actual number of bytes sent, returns -1 on failure.
|
|
|
|
|
|
@@ -403,17 +538,28 @@ sock:close()
|
|
|
|
|
|
- Receive datagram (UDP)
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local recv, data, peer_addr, peer_port = sock:recvfrom(buff, size, opt)
|
|
|
+socket:recvfrom(buff: <bytes>, size: <number>, opt: <table>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Receives a datagram from the UDP socket and gets the sender's address information.
|
|
|
+#### Parameter Description
|
|
|
|
|
|
-Parameters:
|
|
|
-- `buff`: bytes buffer object
|
|
|
-- `size`: Number of bytes to receive
|
|
|
-- `opt` (optional): Option parameters
|
|
|
- - `block`: Whether to block receiving
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| buff | Required. bytes buffer object |
|
|
|
+| size | Required. Number of bytes to receive |
|
|
|
+| opt | Optional. Option parameters |
|
|
|
+
|
|
|
+Options:
|
|
|
+- `block`: Whether to block receiving
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Receives a datagram from the UDP socket and gets the sender's address information.
|
|
|
|
|
|
Return values:
|
|
|
- `recv`: Actual number of bytes received
|
|
|
@@ -450,19 +596,30 @@ end
|
|
|
|
|
|
- Wait for socket events
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local events = sock:wait(events, timeout)
|
|
|
+socket:wait(events: <number>, timeout: <number>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Waits for specified socket events to occur.
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| events | Required. Events to wait for, supports the following event constants |
|
|
|
+| timeout | Required. Timeout (milliseconds), -1 means infinite wait |
|
|
|
|
|
|
-Parameters:
|
|
|
-- `events`: Events to wait for, supports the following event constants:
|
|
|
- - `socket.EV_RECV` (1): Receivable event
|
|
|
- - `socket.EV_SEND` (2): Sendable event
|
|
|
- - `socket.EV_CONN` (2): Connection event (equivalent to EV_SEND)
|
|
|
- - `socket.EV_ACPT` (1): Accept connection event (equivalent to EV_RECV)
|
|
|
-- `timeout`: Timeout (milliseconds), -1 means infinite wait
|
|
|
+Supported event constants:
|
|
|
+- `socket.EV_RECV` (1): Receivable event
|
|
|
+- `socket.EV_SEND` (2): Sendable event
|
|
|
+- `socket.EV_CONN` (2): Connection event (equivalent to EV_SEND)
|
|
|
+- `socket.EV_ACPT` (1): Accept connection event (equivalent to EV_RECV)
|
|
|
+
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Waits for specified socket events to occur.
|
|
|
|
|
|
Return value: Returns the actual event constant value that occurred.
|
|
|
|
|
|
@@ -486,9 +643,19 @@ end
|
|
|
|
|
|
- Close socket
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-sock:close()
|
|
|
+socket:close()
|
|
|
```
|
|
|
+:::
|
|
|
+
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+No parameters required for this function.
|
|
|
+
|
|
|
+#### Usage
|
|
|
|
|
|
Closes the socket and releases resources. Sockets should be closed promptly after use.
|
|
|
|
|
|
@@ -496,16 +663,29 @@ Closes the socket and releases resources. Sockets should be closed promptly afte
|
|
|
|
|
|
- Control socket options
|
|
|
|
|
|
+#### Function Prototype
|
|
|
+
|
|
|
+::: tip API
|
|
|
```lua
|
|
|
-local ok = sock:ctrl(code, value)
|
|
|
+socket:ctrl(code: <number>, value: <number>)
|
|
|
```
|
|
|
+:::
|
|
|
|
|
|
-Sets socket control options to adjust socket parameters such as buffer sizes.
|
|
|
+#### Parameter Description
|
|
|
+
|
|
|
+| Parameter | Description |
|
|
|
+|-----------|-------------|
|
|
|
+| code | Required. Control code constant |
|
|
|
+| value | Required. Control value |
|
|
|
|
|
|
Supported control code constants:
|
|
|
- `socket.CTRL_SET_RECVBUFF` (2): Set receive buffer size (bytes)
|
|
|
- `socket.CTRL_SET_SENDBUFF` (4): Set send buffer size (bytes)
|
|
|
|
|
|
+#### Usage
|
|
|
+
|
|
|
+Sets socket control options to adjust socket parameters such as buffer sizes.
|
|
|
+
|
|
|
Increasing buffer sizes can improve performance in high-throughput scenarios:
|
|
|
|
|
|
```lua
|