LuaSocket
Network support for the Lua language

home · download · installation · introduction · reference


Installation

LuaSocket 2.0.2 uses the new package system for Lua 5.1. All Lua library developers are encouraged to update their libraries so that all libraries can coexist peacefully and users can benefit from the standardization and flexibility of the standard.

Those stuck with Lua 5.0 will need the compat-5.1 module. It is maintained by The Kepler Project's team, and implements the Lua 5.1 package proposal on top of Lua 5.0.

Here we will only describe the standard distribution. If the standard doesn't meet your needs, we refer you to the Lua discussion list, where any question about the package scheme will likely already have been answered.

Directory structure

On Unix systems, the standard distribution uses two base directories, one for system dependent files, and another for system independent files. Let's call these directories <CDIR> and <LDIR>, respectively. For instance, in my laptop, I use '/usr/local/lib/lua/5.0' for <CDIR> and '/usr/local/share/lua/5.0' for <LDIR>. On Windows, sometimes only one directory is used, say 'c:\program files\lua\5.0'. Here is the standard LuaSocket distribution directory structure:

<LDIR>/compat-5.1.lua
<LDIR>/ltn12.lua
<LDIR>/socket.lua
<CDIR>/socket/core.dll
<LDIR>/socket/http.lua
<LDIR>/socket/tp.lua
<LDIR>/socket/ftp.lua
<LDIR>/socket/smtp.lua
<LDIR>/socket/url.lua
<LDIR>/mime.lua
<CDIR>/mime/core.dll

Naturally, on Unix systems, core.dll would be replaced by core.so.

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 at startup:

LUA_INIT=@<LDIR>/compat-5.1.lua

This is only need for Lua 5.0! Lua 5.1 comes with the package system built in, of course.

The other two environment variables instruct the compatibility module to look for dynamic libraries and extension modules in the appropriate directories and with the appropriate filename extensions.

LUA_PATH=<LDIR>/?.lua;?.lua
LUA_CPATH=<CDIR>/?.dll;?.dll

Again, naturally, on Unix systems the shared library extension would be .so instead of .dll.

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.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> socket = require("socket")
> print(socket._VERSION)
--> LuaSocket 2.0.2

Each module loads their dependencies automatically, so you only need to load the modules you directly depend upon:

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> http = require("socket.http")
> print(http.request("http://www.cs.princeton.edu/~diego/professional/luasocket"))
--> homepage gets dumped to terminal