运行简单的lua脚本作为tcp客户端

时间:2016-11-12 17:04:17

标签: lua

运行以下简单的lua脚本但出错了。我在ubuntu上运行lua 5.2。

local host, port = "127.0.0.1", 80
local socket = require("socket")
local tcp = assert(socket.tcp())

tcp:connect(host, port);
--note the newline below
tcp:send("GET / HTTP/1.1\r\n\r\n");

这是错误:

~/learn/lua$ lua te.lua
lua: te.lua:2: module 'socket' not found:
    no field package.preload['socket']
    no file '/usr/local/share/lua/5.2/socket.lua'
    no file '/usr/local/share/lua/5.2/socket/init.lua'
    no file '/usr/local/lib/lua/5.2/socket.lua'
    no file '/usr/local/lib/lua/5.2/socket/init.lua'
    no file '/usr/share/lua/5.2/socket.lua'
    no file '/usr/share/lua/5.2/socket/init.lua'
    no file './socket.lua'
    no file '/usr/local/lib/lua/5.2/socket.so'
    no file '/usr/lib/x86_64-linux-gnu/lua/5.2/socket.so'
    no file '/usr/lib/lua/5.2/socket.so'
    no file '/usr/local/lib/lua/5.2/loadall.so'
    no file './socket.so'
stack traceback:
    [C]: in function 'require'
    te.lua:2: in main chunk
    [C]: in ?

1 个答案:

答案 0 :(得分:1)

安装包管理器luarocks然后再安装套接字包后问题解决了。

以下是页面中的命令

$ wget https://luarocks.org/releases/luarocks-2.4.1.tar.gz
$ tar zxpf luarocks-2.4.1.tar.gz
$ cd luarocks-2.4.1
$ ./configure; sudo make bootstrap
$ sudo luarocks install luasocket
$ lua
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
> require "socket"