Skip to content
Adrian Pistol edited this page Jul 20, 2016 · 1 revision

net

Networking! Yay! if user.Valid() { # store userResponse.User.Id in your user database }

socket, error = net.dial(proto, address)

Returns either a socket as the first return value or an error as the second.

proto is the type of socket, most likely "tcp" or "udp". address self-explanatory.

net.write(socket, content)

Writes content to the socket, without newlines or anything appended to it.

content, error = net.read(socket, count)

Reads at max count bytes and returns the read bytes as a string or an error as its second return value. A common error is "EOF", which just means that there are no bytes left to read at this point in time.

content, error = net.readline(socket)

Reads til a newline appears or an "EOF" error happens. You should most likely just use net.read, since net.readline may cause loss of bytes in some cases.

sv, error = net.listen(proto, address)

Tries to create a listening socket, listening on address. proto could be "tcp" or "udp". address can be something like ":1234" or "127.0.0.1:1234".

socket, error = sv.Accept()

Waits for a connection and returns its socket for further use with the net functions. If error then accepting failed and the socket is not to be used.

You most likely want to run this in a loop and spawn threads handeling the socket.