A simple socket module for lua. It supports: Lua 5.2+.
socket = require "ssocket"
tcpsock = socket.tcp()
ok, err = tcpsock:connect("www.google.com", 80)
if err then
print(err)
end
tcpsock:write("GET / HTTP/1.1\r\n")
tcpsock:write("\r\n")
tcpsock:settimeout(1)
data, err, partial = tcpsock:read(1024)
if err == socket.ERROR_TIMEOUT then
data = partial
end
io.output():write(data)
tcpsock:close()
More examples, see examples/ folder.
$ git clone git://github.com/cofyc/lua-ssocket.git
$ make install
`tcpsock, err = socket.tcp()`
`udpsock, err = socket.udp()`
`readfds, writefds, err = socket.select(readfds, writefds[, timeout=-1])`
`ok, err = tcpsock:connect(host, port)`
`ok, err = tcpsock:connect("unix:/path/to/unix-domain.sock")`
`ok, err = tcpsock:bind(host, port)`
`ok, err = tcpsock:bind("unix:/path/to/unix-domain.sock")`
`ok, err = tcpsock:listen(backlog)`
`tcpsock, err = tcpsock:accept()`
`bytes, err = tcpsock:write(data)`
`data, err, partial = tcpsock:read(size)`
Read specified size of data from socket. This method will not return until it reads exactly the size of data or an error occurs.
In case of success, it returns the data received; in case of error, it returns nil with a string describing the error and the partial data received so far.
`iterator, err = tcpsock:readuntil(pattern, inclusive?)`
This method returns an iterator function that can be called to read the data stream until it sees the specified pattern or an error occurs.
It also takes an optional inclusive argument to control whether to include the pattern string in the returned data string. Default to false.
For example:
local reader = tcpsock:readuntil("\r\n")
while true do
local data, err, partial = reader()
if data then
printf("line: " .. data)
end
end
This iterator function returns the received data right before the specified pattern string in the incoming data stream.
In case of error, it will return nil along with a string describing the error and the partial data bytes that have been read so far.
`ok, err = tcpsock:close()`
Closes the current TCP or stream unix domain socket. It returns the 1 in case of success and returns nil with a string describing the error otherwise.
`ok, err = tcpsock:shutdown(how)`
`fd = tcpsock:fileno()`
`ok, err = tcpsock:setopt(opt, value)`
`value, err = tcpsock:getopt(level, opt)`
`tcpsock:settimeout(timeout)`
Set the timeout in seconds for subsequent socket operations. A negative timeout indicates that timeout is disabled, which is default.
`timeout = tcpsock:gettimeout()`
Returns the timeout in seconds associated with socket. A negative timeout indicates that timeout is disabled, which is default.
`addr, err = tcpsock:getpeername()`
`addr, err = tcpsock:getsockname()`
`ok, err = udpsock:connect(host, port)`
`ok, err = udpsock:connect("unix:/path/to/unix-domain.sock")`
Calling connect() on a datagram socket causes the kernel to record a particular address as this socket’s peer. We can change the peer of a connected datagram socket by issuing a further connect() call.
`ok, err = udpsock:bind(host, port)`
`ok, err = udpsock:bind("unix:/path/to/unix-domain.sock")`
`data, err = udpsock:recv(buffersize)`
Receive up to buffersize bytes from UDP or datagram unix domain socket object.
In case of success, it returns the data received; in case of error, it returns nil with a string describing the error.
`data, addr, err = udpsock:recvfrom(buffersize)`
Works exactly as the udpsock:recv method, except it returns the addr
as extra
return values (and is therefore slightly less efficient) in
case of success.
`ok, err = tcpsock:write(data)`
Writes data on the current UDP or datagram unix domain socket object.
In case of success, it returns true. Otherwise, it returns nil and a string describing the error.
`ok, err = udpsock:send(data, host, port)`
`ok, err = udpsock:send(data, "unix:/path/to/unix-domain.sock")`
Writes data on the current UDP or datagram unix domain socket object to specified address.
In case of success, it returns true. Otherwise, it returns nil and a string describing the error.
`ok, err = udpsock:close()`
Closes the current UDP or datagram unix domain socket. It returns the 1 in case of success and returns nil with a string describing the error otherwise.
`fd = udpsock:fileno()`
`udpsock:settimeout(timeout)`
`timeout = udpsock:gettimeout()`
Module infos:
- socket._VERSION
OPT_* are tcpsock:setopt and tcpsock:getopt parameters:
- socket.OPT_TCP_NODELAY
- socket.OPT_TCP_KEEPALIVE
- socket.OPT_TCP_REUSEADDR
SHUT_* are tcpsock:shutdown() parameters:
- socket.SHUT_RD
- socket.SHUT_WR
- socket.SHUT_RDWR
ERROR_* are predefined error strings, which can be used to detect errors:
- socket.ERROR_TIMEOUT
- socket.ERROR_CLOSED
- socket.ERROR_REFUSED