Socket
The socket library implements (most of) the LuaSocket API.
On our platform, we treat all sockets as non-blocking sockets. This means
you should expect any socket methods to return nil, "timeout"
. To duplicate blocking
socket behavior, you can utilize socket.select
. See the example below.
local socket = require "socket"
local tcp_client = socket.tcp()
local ip, port = "0.0.0.0", 80
local res, err = tcp_client:connect(ip, port)
if err == "timeout" then
-- The second argument here is the "sender" position
socket.select({}, {tcp_client})
res, err = tcp_client:connect(ip, port)
end
assert(res, err)
assert(tcp_client:send("hello world!")
local reply, err = tcp_client:receive()
if err == "timeout" then
-- the first argument here is the "receiver" position
socket.select({tcp_client})
reply, err = tcp_client:receive()
end
print(reply)
In almost all cases it is preferable to use the cosock.socket APIs provided instead of anything provided by this module.
- socket.bind(address, port, backlog)
Bind to local port
Not implemented
- Parameters
address (
str
) – address to bind onport (
number
) – port to bind onbacklog (
number
) – optional param, max queued client connections
- Returns
server table
- Return type
tcp
- socket.connect(address, port, locaddr, locport)
Returns a tcp client object
- Parameters
address (
str
) – Address to connect toport (
number
) – Port to connect tolocaddr (
str
) – Local address to connect fromlocport (
number
) – Local port to connect from
- class socket.ssl
- class socket.st_socket
- static sleep(seconds)
Sleep for an arbitrary amount of time
- Parameters
seconds (
number
) – Sleep duration
- static gettime()
Get the system time
- Returns
Milliseconds since the Unix epoch
- Return type
number
- class socket.timer
Event stream of timers ready notifications
Note
This socket is usually not used directly by drivers, but via event handlers in the Driver.