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 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'.

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 [, backlog])

Binds a master object to address and port on the local host, transforming it into a server object. Server objects support the accept, getsockname, setoption, settimeout, and close methods.

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. The optional parameter backlog, which defaults to 1, 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.

Note: The function socket.bind is available and is a short for socket.tcp followed by the bind method.

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 short for socket.tcp followed by the connect method.

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.

Note: Naturally, for a server object, the address and port returned are those passed to the bind method. If the port value passed to bind was 0, the OS assigned ephemeral port is returned. For client objects, both the address and port are ephemeral and these are the values returned.

client:receive([pattern1, pattern2, ... patternN])

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

The parameters pattern1, pattern2, ... patternN can be any of the following:

The method returns one value for each pattern, followed by a single error code that can be nil in case of success, 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.

Note: In case of error, the method always return everything it managed to download before the error condition was met.

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.

The method returns the number of bytes accepted by the transport layer, followed by an error code. The error code is nil if the operation completed with no errors, 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.

Note: The return values for the send method have been changed in LuaSocket 2.0! In previous versions, the method returned only the error message. Since returning nil in case of success goes against all other LuaSocket methods and functions, the send method been changed for the sake of uniformity.

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.

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.