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")

Functions

init ([buffer=0x100000]) Initialize the socket module
shutdown () Disable the socket module.
tcp () Return a TCP socket.
udp () Return an UDP socket.
addTrustedRootCA (cert) Add a trusted root CA to the certChain.

All sockets

:bind (port) Bind a socket.
:close () Close an existing socket.
:getpeername () Get some informations from a socket.
:getsockname () Get some local informations from a socket.
:setBlocking ([block=true]) Set if the socket should be blocking.

TCP Sockets

:accept () Accept a connection on a server.
:connect (host, port[, ssl=false]) Connect a socket to a server.
:listen ([max=16]) Open the socket for connections.
:receive ([size="l"]) Receive some data from the socket.
:send (data) Send some data over the TCP socket.

UDP sockets

:receivefrom ([count=8191]) Receive a datagram from the UDP object.
:sendto (data, host, port) Send a datagram to the specified IP and port.


Functions

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

  1. boolean false in case of error
  2. number/string error code/message
shutdown ()
Disable the socket module.
tcp ()
Return a TCP socket.

Returns:

    TCPMaster TCP socket

Or

  1. nil in case of error
  2. string error message
udp ()
Return an UDP socket.

Returns:

    UDPMaster UDP socket

Or

  1. nil in case of error
  2. string error message
addTrustedRootCA (cert)
Add a trusted root CA to the certChain.

Parameters:

Returns:

    boolean true if everything went fine

Or

  1. nil in case of error
  2. number error code

All sockets

: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:

  1. string IP
  2. number port
:getsockname ()
Get some local informations from a socket.

Returns:

  1. string IP
  2. number port
:setBlocking ([block=true])
Set if the socket should be blocking.

Parameters:

  • block boolean if false, the socket won't block (default true)

TCP Sockets

: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

  1. boolean false if failed
  2. 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

  1. nil in case of error
  2. integer error code
:send (data)
Send some data over the TCP socket.

Parameters:

Returns:

    number amount of data sent

Or

  1. nil in case of error
  2. integer/string error code/message

UDP sockets

: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:

  1. string data
  2. string IP address of the sender
  3. integer port number of the sender

Or

  1. nil in case of error or no datagram to receive
  2. 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

  1. boolean false in case of error
  2. string error message
generated by LDoc 1.4.3 Last updated 2017-06-05 14:35:27