LuaSocket
Network support for the Lua language

home · download · instalation · introduction · reference


Instalation

LuaSocket 2.0 uses the new package proposal for Lua 5.1, throught the compatibility module Compat-5.1 released in conjunction with Roberto Ierusalimschy and The Kepler project. The proposal was considered important enough by the community to justify early adoption. All Lua library developers are encouraged to change their libraries in preparation for the release of Lua 5.1.

As far as LuaSocket is concerned, this means that whoever is deploying a solution that uses LuaSocket has a lot of freedom. Here we describe only the standard distribution. If the standard doesn't meet your needs, we refer you to the Lua discussion list, where any quesetion about the package scheme will likely be answered promptly.

Directory structure

The new package scheme has a root directory for the libraries installed on a given system. Let's call this directory <ROOT>. On my system, this is the /usr/local/share/lua/5.0 directory. Here is the standard LuaSocket distribution directory structure:

<ROOT>/compat-5.1.lua
<ROOT>/socket.lua
<ROOT>/lsocket.dll
<ROOT>/mime.lua
<ROOT>/lmime.dll
<ROOT>/ltn12.lua
<ROOT>/socket/http.lua
<ROOT>/socket/tp.lua
<ROOT>/socket/ftp.lua
<ROOT>/socket/smtp.lua
<ROOT>/socket/url.lua

Naturally, on Unix systems, lsocket.dll and lmime.dll would be replaced by lsocket.so and lmime.so. In Mac OS X, they would be replaced by lsocket.dylib and lmime.dylib.

In order for the interpreter to find all LuaSocket components, three environment variables need to be set. The first environment variable tells the interpreter to load the compat-5.1.lua module.

LUA_INIT=@<ROOT>/compat-5.1.lua
The other two environment variables instruct the compatibility module to look for dynamic libraries and modules in the appropriate directories and with the appropriate filename extensions.
LUA_PATH=<ROOT>/?.lua;?.lua
LUA_CPATH=<ROOT>/?.dll;?.dll

Again, naturally, in Unix the shared library extension would be .so instead of .dll and on Mac OS X they would be .dylib

Using LuaSocket

With the above setup, and an interpreter with shared library support, it should be easy to use LuaSocket. Just fire the interpreter and use the require function to gain access to whatever module you need:

Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> socket = require("socket")
> print(socket.VERSION)
--> LuaSocket 2.0 (beta3)

Each module loads their dependencies automatically, so you only need to load the modues you are directly dependent upon.

Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> http = require("socket.http")
> print(http.get("http://www.tecgraf.puc-rio.br/luasocket"))
--> homepage gets dumped to terminal