Installing Lua socket library

纵然是瞬间 提交于 2019-12-10 03:49:06

问题


Either I'm overtired or blind. I want to learn networking with Lua and therefore I have to install the socket lib, so I can require it easily, but I don't know, which files I should "require". The example says:

local socket = require("socket")

but as I said, I don't know which files I should include, if I use socket.lua it doesn't work and I get: No files found.

I got the lib from here: Lua socket download

Or, is there another way to install the socket lib?


回答1:


When you load a module with require Lua uses the package paths to determine where to look for the module. Have a look at the Modules section of the Lua manual. Specifically, the section on package.path and package.cpath.

package.path: The path used by require to search for a Lua loader (.lua modules)
package.cpath: The path used by require to search for a C loader (.so/.dll modules)

You can check what the current paths are:

print(package.path..'\n'..package.cpath)

If you install LuaSocket into a location within your current package paths Lua should be able to locate and load it.

Alternatively, you can modify the package paths before calling require. For example, if you create a folder for your project and extract the LuaSocket library to a sub-folder called libs within your project folder:

Project
|
> libs
     |
     > lua
         |
         > socket         
     > socket
     > mime

You can set the package paths relative to your project before you require the socket library (substitute /?.dll for /?.so on Linux):

package.path = package.path..';./libs/lua/?.lua'
package.cpath = package.cpath..';./libs/socket/?.dll;./libs/mime/?.dll'
local socket = require 'socket'



回答2:


use this command on your linux system :

#luarocks install luasocket

the use the next command to see the paths CONFIGURATION

#luarocks 

You need to use default linux account (see: CONFIGURATION) :

$lua 
> socket = require ("socket")

or use this:

> socket = require 'socket'


来源:https://stackoverflow.com/questions/10356709/installing-lua-socket-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!