LuaSocket
Network support for the Lua language

home · download · introduction · reference


TCP

socket.tcp()

Creates and returns a TCP master object. A master object can be transformed into a server object with the method listen (after a call to bind) or into a client object with the method connect. The only other method supported by a master object is the close method.

In case of success, a new master object is returned. In case of error, nil is returned, followed by an error message.

server:accept()

Waits for a remote connection on the server object and returns a client object representing that connection.

If a connection is successfully initiated, a client object is returned. If a timeout condition is met, the method returns nil followed by the error string 'timeout'. Other errors are reported by nil followed by a message describing the error.

Note: calling socket.select with a server object in the receive parameter before a call to accept does not guarantee accept will return immediately. Use the settimeout method or accept might block until another client shows up.

master:bind(address, port)

Binds a master object to address and port on the local host.

Address can be an IP address or a host name. Port must be an integer number in the range [0..64K). If address is '*', the system binds to all local interfaces using the INADDR_ANY constant. If port is 0, the system automatically chooses an ephemeral port.

In case of success, the method returns 1. In case of error, the method returns nil followed by an error message.

Note: The function socket.bind is available and is a shortcut for the creation server sockets.

master:close()
client:close()
server:close()

Closes a TCP object. The internal socket used by the object is closed and the local address to which the object was bound is made available to other applications. No further operations (except for further calls to the close method) are allowed on a closed socket.

Note: It is important to close all used sockets once they are not needed, since, in many systems, each socket uses a file descriptor, which are limited system resources. Garbage-collected objects are automatically closed before destruction, though.

master:connect(address, port)

Attempts to connect a master object to a remote host, transforming it into a client object. Client objects support methods send, receive, getsockname, getpeername, settimeout, and close.

Address can be an IP address or a host name. Port must be an integer number in the range [1..64K).

In case of error, the method returns nil followed by a string describing the error. In case of success, the method returns 1.

Note: The function socket.connect is available and is a shortcut for the creation of client sockets.

Note: Starting with LuaSocket 2.0, the settimeout function affects the behavior of connect, causing it to return in case of a timeout error.

client:getpeername()

Returns information about the remote side of a connected client object.

Returns a string with the IP address of the peer, followed by the port number that peer is using for the connection. In case of error, the method returns nil.

Note: It makes no sense to call this method on server objects.

client:getsockname()
server:getsockname()

Returns the local address information associated to the object.

The method returns a string with local IP address and a number with the port. In case of error, the method returns nil.

master:listen(backlog)

Specifies the socket is willing to receive connections, transforming the object into a server object. Server objects support the accept, getsockname, setoption, settimeout, and close methods.

The parameter backlog specifies the number of client connections that can be queued waiting for service. If the queue is full and another client attempts connection, the connection is refused.

In case of success, the method returns 1. In case of error, the method returns nil followed by an error message.

client:receive([pattern])

Reads data from a client object, according to the specified read pattern. Patterns follow the Lua file I/O format, and the difference in performance between all patterns is negligible.

Pattern can be any of the following:

If successful, the method returns the received pattern. In case of error, the method returns nil followed by an error message which can be the string 'closed' in case the connection was closed before the transmission was completed or the string 'timeout' in case there was a timeout during the operation. Also, after the error message, the function returns the partial result of the transmission.

Important note: This function was changed severely. It used to support multiple patterns (but I have never seen this feature used) and now it doesn't anymore. Partial results used to be returned in the same way as successful results. This last feature violated the idea that all functions should return nil on error. Thus it was changed too.

client:send(string1 [, string2, ... stringN])

Sends data through client object.

All parameters should be strings. For small strings, it is always better to concatenate them in Lua (with the '..' operator) and pass the result to LuaSocket instead of passing several independent strings.

If successful, the method returns the number of bytes accepted by the transport layer. In case of error, the method returns nil, followed by an error message, followed by the partial number of bytes accepted by the transport layer. The error message can be 'closed' in case the connection was closed before the transmission was completed or the string 'timeout' in case there was a timeout during the operation.

Important note: The return values for the send method have been changed in LuaSocket 2.0 alpha and again in the beta (sorry)! In previous versions, the method returned only the error message. Since returning nil in case of success was nonsense, in alpha the first return value became the number of bytes sent. Alas, it wasn't returning nil in case of error. So it was changed again in beta.

client:setoption(option [, value])
server:setoption(option [, value])

Sets options for the TCP object. Options are only needed by low-level or time-critical applications. You should only modify an option if you are sure you need it.

Option is a string with the option name, and value depends on the option being set:

The method returns 1 in case of success, or nil otherwise.

Note: The descriptions above come from the man pages.

client:settimeout(value [, mode])
server:settimeout(value [, mode])

Changes the timeout values for the object. By default, all I/O operations are blocking. That is, any call to the methods send, receive, and accept will block indefinitely, until the operation completes. The settimeout method defines a limit on the amount of time the I/O methods can block. When a timeout is set and the specified amount of time has elapsed, the affected methods give up and fail with an error code.

The amount of time to wait is specified as the value parameter, in seconds. There are two timeout modes and both can be used together for fine tuning:

The nil timeout value allows operations to block indefinitely. Negative timeout values have the same effect.

Note: although timeout values have millisecond precision in LuaSocket, large blocks can cause I/O functions not to respect timeout values due to the time the library takes to transfer blocks to and from the OS and to and from the Lua interpreter. Also, function that accept host names and perform automatic name resolution might be blocked by the resolver for longer than the specified timeout value.

Note: The old timeout method is deprecated. The name has been changed for sake of uniformity, since all other method names already contained verbs making their imperative nature obvious.

client:shutdown(mode)

Shuts down part of a full-duplex connection.

Mode tells which way of the connection should be shut down and can take the value:

This function returns 1.