首页 > 代码库 > 把luasocket集成到c++中

把luasocket集成到c++中

建一个项目pro_test,创建一个运行目录test;

把luasocket/src文件夹中的*.lua拷贝到test/src文件夹中;

把socket.dll,mime.dll,lua5.1.dll拷贝到test文件夹中;

把socket.lua改为socket_wrap.lua,因为socket.lua和socket.dll重名了;

 

main.cpp:

#include <stdio.h>

extern "C" 
{
    #include "luasocket.h"
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
    #include "luaconf.h"
};

int main(int narg, char* args[])
{
    lua_State* L = luaL_newstate();
    luaopen_base(L);
    luaL_openlibs(L);

    luaopen_socket_core(L);

    int ret = luaL_dofile(L, args[1]);
    if(ret != 0)
    {
        printf("%s", lua_tostring(L, -1));
    }
    return 0;
}

main.lua:

package.path = D:/xxx/test/src/?.lua;
require("socket_wrap")
local socket = require("socket")
local server = assert(socket.bind("*", 0))
local ip, port = server:getsockname()
print("server open on port " .. port)
local client = server:accept();

 

把luasocket集成到c++中