LuaSocket: TCP/IP support for the Lua language


Contents

What is LuaSocket?

LuaSocket is a Lua extension library that allows the use the most important features of the TCP/IP socket layer within the Lua language.

It can be used by any Lua application desiring access to TCP/IP communication, once it has been properly linked with and initialized by the interpreter running the application. The code has been tested and runs well on the WinSock2 and 4.3BSD platforms.

The library is available under the same terms as the Lua language, that is, it can be used at no cost for both academic and commercial purposes.

Copyright (C) 2000 TeCGraf, PUC-Rio. All rights reserved.
Author: Diego Nehab

Introduction

To have the TCP/IP made functions available to a Lua script, the interpreter running the script must be linked to the socklib library. The functions are registered in the Lua scope when the interpreter calls the C function lua_socklibopen, the only C function exported by the library. The scripts can then use all registered functions.

To connect to a server, a client application creates a client socket object with a call to the function connect. Once this object is created, the user can use the functions send and receive to exchange information with the server. After all data exchange is done, the client application can close the connection by calling the close function on the client socket.

On the server side, a server application binds to an address with a call to the bind function, which returns a server socket object. The server application can then accept remote connections on the server socket, with calls to the accept function. This function returns a client socket, through which the server application can communicate with the client application that attempted connection. Both server and client sockets can be closed with the close function.

All functions are available both as stand-alone functions and as methods of the socket objects. For example, the function call send(socket,"test") is equivalent to the function call socket:send("test"), where socket is a client socket.

Download

LuaSocket version 1.0 is now available for download! It is compatible with Lua 4.0 and has been tested on Windows 98, Windows 2000, Linux, AIX, SunOS and Solaris.

luasocket-1.0.tar.gz
luasocket-1.0.zip

Reference Manual

accept(socket)

Returns a client socket object, representing a client attempting connection on the server socket socket. The function blocks until a connection attempt is detected.

bind(address, port [, backlog])

Binds to the address address and port port on the local host. Address can be an IP address or a host name. The optional parameter backlog (default value 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 function returns a server socket, on which the operations accept, close and listen are permitted. In case of error, the function returns nil followed by a string describing the error.

close(socket)

Closes the socket socket. No further operations are allowed on a closed socket. In case socket is a server socket, the address to which it is bound is made available to other applications. It is important to close all used sockets once they are not needed, since, in many systems, each socket uses a file descriptor, which is a limited system resource.

connect(address, port)

Attempts connection to address address and port port. Address can be an IP address or a host name. In case of success, the function returns a client socket on which the operations send, receive and close are permitted. In case of error, the function returns nil followed by a string describing the error.

listen(socket, backlog)

Changes the backlog parameter of the server socket socket.

send(socket, string1 [, string2, ... stringN])

Sends the strings string1, string2, ... stringN through the client socket socket. The function returns an error code, which is nil in case of success, the string `closed' in case the connection was closed before the transmission was complete or the string `timeout' in case there was a timeout during the operation. After the error code, the function returns the number of bytes accepted by the transport layer.

receive(socket [, pattern1, pattern2, ... patternN])

Receives pattern1, pattern2, ... patternN from the client socket socket. A pattern can be one of the following:

The function returns one string 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 complete or the string `timeout' in case there was a timeout during the operation.

timeout(socket, value [, mode])

Changes the timeout values for the socket socket. By default, all I/O operations are blocking. That is, any call to the functions send and receive will block indefinetely, until the operation completes. The timeout function defines a limit on the ammount of time the functions can block, specified as the value parameter, in seconds. There are two timeout modes, which can be used together:


Last modified by Diego Nehab
Wed Nov 8 16:32:38 EDT 2000