luasocket系列: luasocket hello world!

LuaSocket是一个针对Lua的扩展库,包含TCP和UDP支持。2.0.2版本兼容Lua 5.1,已在多种平台成功测试。在Windows环境下,需链接ws2_32.lib以使用winsock API。此库小巧且易学习,适用于跨平台网络编程,提供面向对象编程方式。
摘要由CSDN通过智能技术生成


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 文件并部分修改:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值