WiFiMCU Reference Book

Function list

Function Definition
net.new() Create a new socket, set the socket and transmission protocol
net.start() Start the socket, set remote port, remote ip address, or local port according to the socket and transmission protocol
net.on() Register the callback functions for socket events
net.send() Send data
net.close() Close socket
net.getip() Get the ip address and port of the client socket.

Constant

Constant Definition
net.TCP TCP protocol
net.UDP UDP protocol
net.SERVER Server type
net.CLIENT Client type

net.new()

Description

Create a new socket, set the socket and protocol type. Max 4 server and Max 4 client can be setup in WiFiMCU. If the socket type is Server, max number of 5 clients are allowed to connect.

Syntax

skt=net.new(protocol,type)

Parameters

protocol: The transmission protocol, must be one of the two: net.TCP, net.UDP

type: socket type, must be one of the two: net.SERVER, net.CLIENT

Returns

skt: the andle for this socket

Examples

-skt = net.new(net.TCP,net.SERVER)

-skt2 = net.new(net.UDP,net.CLIENT)

net.start()

Description

Start the socket, set remote port, remote ip address, or local port according to the socket and transmission protocol.

Syntax

net.start(socket, localport)

net.start(socket, remoteport, "domain", [local port])

Parameters

socket: The socket handle returned from net.new()

localport: If the socket type is net.SERVER, It’s the local binded port for this socket.

remoteport: If the socket type is net.CLIENT, It’s the remote server port.

"domain": If the socket type is net.CLIENT, it’s the domain name string for remote server. The remote server’s ip address can be used too.

[local port]: Optinal, if the socket type is net.CLIENT, [local port] set the local binded port for the socket. If ignored, a random port would be assigned.

Returns

nil

Examples

-skt = net.new(net.TCP,net.SERVER)

-skt2 = net.new(net.UDP,net.CLIENT)

-net.start(skt, 80)

-net.start(skt2,9000,'11.11.11.2', 8000)

net.on()

Description

Register the callback functions for socket events.

Syntax

net.on(socket,event,func_cb)

Parameters

socket: The socket handle returned from net.new()

event: If the socket type is net.SERVER, event should be one of the following:

  “accept”(TCP server socket only), “receive”, “sent”, “disconnect”.

  If the socket type is net.CLIENT, event should be one of the following:

  “connect(TCP client socket only)”, “receive”, “sent”, “disconnect”, ”dnsfound”.

func_cb: Callback function for different events. The function parameters diff from events.

  “accept”: TCP server socket only. If the tcp server accept a tcp client connection request, the function will be called. Function prototype is: func_cb(clt, ip, port). “clt” is the tcp client socket handle, “ip” is the client ip address, “port” is the client’s port.

  “receive”: If data arrived on the assigned socket, the function will be called. Function prototype is: func_cb(clt, data). “clt” is the socket handle, “data” is the received data.

  “sent”: When data had sent succeffuly on the assigned socket, the function will be called. Function prototype is: func_cb(clt). “clt” is the socket handle.

  “disconnect”: If the client socket is disconnected from server or some errors happened, the function will be called. Function prototype is: func_cb(clt). “clt” is the socket handle.

  “connect”: TCP Client socket only. When the client socket connects to the remote server successfully, the function will be called. Function prototype is: func_cb(clt). “clt” is the socket handle.

  “dnsfound”: TCP or UDP Client socket only. When the DNS operations has finished, the function will be called. Function prototype is: func_cb(clt, ip). “clt” is the socket handle, “ip” is the ip address for the domain.

Returns

nil

Examples

-clt = net.new(net.TCP,net.CLIENT)

-net.on(clt,"dnsfound",function(clt,ip) print("dnsfound clt:"..clt.." ip:"..ip) end)

-net.on(clt,"connect",function(clt) print("connect:clt:"..clt) end)

-net.on(clt,"disconnect",function(clt) print("disconnect:clt:"..clt) end)

-net.on(clt,"receive",function(clt,d) print("receive:clt:"..clt.."data:"..d) end)

-net.start(clt,9003,"11.11.11.2")

net.send()

Description

Send data.

Syntax

net.send(socket, data, [func_cb])

Parameters

socket: The socket handle returned from net.new()

data: Data to be sent.

[func_cb]: Optinal, “sent” eventcall back function. When data had sent succeffuly on the assigned socket, the function will be called. Function prototype is: func_cb(clt). “clt” is the socket handle.

Returns

nil

Examples

-net.send(clt,"hello")

net.close()

Description

Close socket, release the resource of the socket.

Syntax

net.close(socket)

Parameters

socket: The socket handle returned from net.new()

Returns

nil

Examples

-skt = net.new(net.TCP,net.SERVER)

-net.close(skt)

net.getip()

Description

Get the ip address and port of the client socket.

Syntax

ip, port = net.getip(socket)

Parameters

socket: The socket handle returned from net.new(). The socket handle should be a client socket.

Returns

ip: the ip address for the socket.

port: the port for the socket.

Examples

-ip, port = net.getip(clt)