Module ctr.socket
The socket module.
Almost like luasocket, but for the TCP part only.
The UDP part is only without connection.
All sockets are not blocking by default.
Usage:
local socket = require("ctr.socket")
-
init ([buffer=0x100000])
-
Initialize the socket module
Parameters:
- buffer
number
size (in bytes), must be a multiple of 0x1000
(default 0x100000)
Returns:
boolean
true
if everything went fine
Or
-
boolean
false
in case of error
-
number/string
error code/message
-
shutdown ()
-
Disable the socket module.
-
tcp ()
-
Return a TCP socket.
Returns:
TCPMaster
TCP socket
Or
-
nil
in case of error
-
string
error message
-
udp ()
-
Return an UDP socket.
Returns:
UDPMaster
UDP socket
Or
-
nil
in case of error
-
string
error message
-
addTrustedRootCA (cert)
-
Add a trusted root CA to the certChain.
Parameters:
Returns:
boolean
true
if everything went fine
Or
-
nil
in case of error
-
number
error code
-
:bind (port)
-
Bind a socket. The socket object become a socketServer object.
Parameters:
- port
number
the port to bind the socket on.
-
:close ()
-
Close an existing socket.
-
:getpeername ()
-
Get some informations from a socket.
Returns:
-
string
IP
-
number
port
-
:getsockname ()
-
Get some local informations from a socket.
Returns:
-
string
IP
-
number
port
-
:setBlocking ([block=true])
-
Set if the socket should be blocking.
Parameters:
- block
boolean
if
false
, the socket won't block
(default true)
-
:accept ()
-
Accept a connection on a server.
Returns:
TCPClient
tcp client object, or nil.
-
:connect (host, port[, ssl=false])
-
Connect a socket to a server. The TCP object becomes a TCPClient object.
Parameters:
- host
string
address of the host
- port
number
port of the server
- ssl
boolean
use SSL if
true
(default false)
Returns:
boolean
true if success
Or
-
boolean
false if failed
-
string
error string
-
:listen ([max=16])
-
Open the socket for connections.
Parameters:
- max
number
maximum number of simultaneous connections
(default 16)
-
:receive ([size="l"])
-
Receive some data from the socket.
If no data is avaible, it returns an empty string (non-blocking).
Parameters:
- size
number/string
amount of bytes to receive; or
"a" to receive everything,
"l" to receive the next line, skipping the end of line,
"L" to receive the next line, keeping the end of line.
(default "l")
Returns:
string
data
Or
-
nil
in case of error
-
integer
error code
-
:send (data)
-
Send some data over the TCP socket.
Parameters:
Returns:
number
amount of data sent
Or
-
nil
in case of error
-
integer/string
error code/message
-
:receivefrom ([count=8191])
-
Receive a datagram from the UDP object.
Parameters:
- count
number
maximum amount of bytes to receive from the datagram. Must be lower than 8192.
(default 8191)
Returns:
-
string
data
-
string
IP address of the sender
-
integer
port number of the sender
Or
-
nil
in case of error or no datagram to receive
-
string
error message
-
:sendto (data, host, port)
-
Send a datagram to the specified IP and port.
Parameters:
- data
string
data to send
- host
string
IP/hostname of the recipient
- port
number
port number of the recipient
Returns:
boolean
true in case of success
Or
-
boolean
false in case of error
-
string
error message