LuaSocket 简介:
LuaSocket is a Lua extension library that is composed by two parts:
a C core that provides support for the TCP and UDP transport layers, and a set of Lua modules that add support for functionality commonly needed by applications that deal with the Internet.
the core support has been implemented so that it is both efficient and simple to use. It is available to any Lua application once it has been properly initialized by the interpreter in use.
The code has been tested and runs well on several Windows and Unix platforms.
LuaSocket version 2.0.2 is now available for download! It is compatible with Lua 5.1, and has been tested on Windows XP, Linux, and Mac OS X.
2.0.2 is just a bug-fix/update release.
LuaSocket 是一个Lua的拓展库。包括两个部分:C语言编写提供Tcp和Udp 传输层协议的核心模块和支持处理网络Lua模块。
核心模块被高效且简单的是实现。仅仅要嵌入lua解释器就可以使用。核心模块已经在多个windows和unix 操作系统测试和运行。
LuaSocket 2.0.2版本和Lua 5.1.x版本兼容,已经在window xp ,linux 和 Mac os X操作系统测试过。
LuaSocket 作为一个bug 修复版本释放。
LuaSocket 使用:
本文使用windows 7 操作系统和使用vs2010 工具。所以仅仅测试winsock api 部分。
1,在导入头文件和源文件时,移除usocket等相关unix操行系统下的文件。
2 在wsocket.h 中加入 #pragma comment(lib,"ws2_32.lib") 导入 winsock库,否则在link过程中,会找到socket api相关函数。
luasocketsample.cpp 文件内容如下:
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "luasocket.h" //使用LuaSocket 库仅仅需要包含的头文件。
}
int main(int argc, char **argv)
{ //创建lua_State
lua_State *L = lua_open(); /* create state */
//注册标准库
luaL_openlibs (L);
//注册LuaSocket库
luaopen_socket_core(L);
//执行sample_1.lua
luaL_dofile (L, "luasocketsample.lua");
//关闭lua_State
lua_close(L);
return 1;
}
在LuaSocket 源代码中有例子,以下例子取自echosrvr.lua 文件并部分修改: