我想实现像在我的 Lua 构建中嵌入套接字功能一样的功能。所以我不需要再复制socket.core.dll(只是为了好玩)。
我搜索邮件列表,看到一些人讨论这个主题, http://lua-users.org/lists/lua-l/2005-10/msg00269.html
但我对详细步骤有疑问,谁可以给我更改 lua 和 luasocket 代码以使它们协同工作(而不是使用 dll 方法)的详细步骤。
我在 Windows XP 和 VC2008 中尝试了以下步骤:
1)将luasocket代码复制到Lua项目中。
2)添加一些代码
static const luaL_Reg lualibs[] = {
{"", luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_DBLIBNAME, luaopen_debug},
{LUA_SOCKETLIBNAME, luaopen_socket_core}, // add this line
{LUA_MIMELIBNAME, luaopen_socket_core}, // add this line
{NULL, NULL}
};
Run Code Online (Sandbox Code Playgroud)
3)构建项目,并运行它。
当我输入时print(socket._VERSION),它显示luasocket 2.0.2,它是正确的。
当我输入时print(socket.dns.toip("localhost")),它显示127.0.0.1 table: 00480AD0,它也是正确的。
但是当我尝试使用其他功能(例如绑定)时,它无法工作。
谁能告诉我原因?
我正在为 Lua 安装 luasocket 模块,但收到以下错误消息:
[root@localhost local]# wget http://files.luaforge.net/releases/luasocket/luasocket/luasocket-2.0.2/luasocket-2.0.2.tar.gz
[root@localhost local]# tar zxvf luasocket-2.0.2.tar.gz
[root@localhost local]# cd luasocket-2.0.2
[root@localhost luasocket-2.0.2]# make && make install
cd src; make all
make[1]: Entering directory `/usr/local/luasocket-2.0.2/src'
gcc -DLUASOCKET_DEBUG -pedantic -Wall -O2 -fpic -c -o luasocket.o luasocket.c
luasocket.c:20:17: error: lua.h: No such file or directory
luasocket.c:21:21: error: lauxlib.h: No such file or directory
luasocket.c:24:24: error: compat-5.1.h: No such file or directory
In file included from luasocket.c:30:
luasocket.h:30: error: expected ‘)’ before ‘*’ token
In file …Run Code Online (Sandbox Code Playgroud) 我在使用 lua 时遇到了麻烦。
我需要通过 GET 向网站发送请求并从网站获取响应。
Atm 我只有这个:
local LuaSocket = require("socket")
client = LuaSocket.connect("example.com", 80)
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\n\r\n")
while true do
s, status, partial = client:receive('*a')
print(s or partial)
if status == "closed" then
break
end
end
client:close()
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能得到服务器的响应?
我想向这个网站发送一些信息并获得页面的结果。
有任何想法吗 ?
我使用 LuaForWindows(最新版本),我已经阅读了这个和这个答案以及我在lua-users.org的邮件列表中可以找到的所有内容。我尝试过的(大多数)站点只响应 301 或 302。我创建了一个示例批处理脚本,它从他们的手册页下载(一些)OpenGL 2.1 参考。
@ECHO OFF
FOR /F "SKIP=5" %%# IN ( %~fs0 ) DO lua -l socket.http -e "print(socket.http.request('https://www.opengl.org/sdk/docs/man2/xhtml/%%#.xml'))"
GOTO:EOF
glAccum
glActiveTexture
glAlphaFunc
glAreTexturesResident
glArrayElement
glAttachShader
glBegin
glBeginQuery
glBindAttribLocation
glBindBuffer
Run Code Online (Sandbox Code Playgroud)
最重要的部分是:
print(require('socket.http').request('https://www.opengl.org/sdk/docs/man2/xhtml/glAccum.xml')) -- added glAccum so you can run it
Run Code Online (Sandbox Code Playgroud)
这总是返回 301。从其他随机页面下载时,这也会发生在我身上。(我没有注意到它们,所以我不能给出一个列表,但我碰巧发现其中一些使用了 cloudflare。)
如果我使用URL和openConnection()在 Java 中编写等效的下载器,它不会重定向。
我已经尝试手动跟踪重定向(设置引用者和内容)并使用“通用”方式。正如其他答案中所述的大多数提示一样。
每当我运行我的应用程序时,我遇到此错误.错误是:
loop or previous error loading module 'socket'.
导致此错误的代码是:
socket = require("socket").
在第一次出现此错误lua_pcall.这是调用它的函数:
void startTerminal(int port, char host[80])
{
lua_State *L = lua_open();
/* Open Lua Library */
luaL_openlibs(L);
/* Choose the lua file that will run */
if(luaL_loadfile(L, "socket.lua")) {
lfatal(L, "luaL_loadfile() failed");
}
/* Start lua file */
if(lua_pcall(L, 0, 0, 0)) {
lfatal(L, "lua_pcall()");
}
/* Get connect function */
lua_getglobal(L, "connect");
if(!lua_isfunction(L, -1)) {
lua_pop(L, 1);
lfatal(L, "lua_isfunction() failed");
}
/* Setup …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用LuaSocket库测试Corona SDK中是否存在互联网连接.
我找到了这个解决方案
function test()
local connection = socket.tcp()
connection:settimeout(1000)
local result = connection:connect("www.google.com", 80)
connection:close()
if (result) then return true end
return false
end
Run Code Online (Sandbox Code Playgroud)
但它有一个问题:如果连接错误/不稳定,程序将被阻塞,直到套接字运行(持续不同的秒数).
所以我试着这样:
connection:settimeout(1000, 't')
Run Code Online (Sandbox Code Playgroud)
但它非常不准确(在网络滞后的情况下会返回false).有一个更好的方法?
也许让套接字没有阻塞?
更新2: 我尝试了这段代码,但我真的不明白它是否有意义......
local socket = require("socket")
function test(callback, timeout)
if timeout == nil then timeout = 1000 end
local connection = socket.tcp()
connection:settimeout(0)
connection:connect("www.google.com", 80)
local t
t = timer.performWithDelay( 10, function()
local r = socket.select({connection}, nil, 0)
if r[1] or timeout == 0 then
connection:close()
timer.cancel( t ) …Run Code Online (Sandbox Code Playgroud) 我正在尝试安装 luasocket,但我找不到任何有关如何执行此操作的信息。我是从luaforge luasocket-2.0.2.tar.gz存档下载的,但是没有configure文件,只有MVS解决方案。那么,如何获得编译的库呢?
我用的时候
local socket = require("socket.core")
Run Code Online (Sandbox Code Playgroud)
它工作正常,DLL位于"dir/socket/core.dll"但是当我移动dll说
"dir/folder/core.dll"并使用
local socket = require("folder.core.")
Run Code Online (Sandbox Code Playgroud)
它返回它被发现然而它找不到folder.core中的特定模块.
如何在socket.core要求之外使用Luasocket?
谢谢!
我正在尝试使用LuaSocket来完成我正在进行的项目.我选择UDP作为我的协议.
在线寻找文档和教程,我试图创建一个客户端 - 服务器对进行测试和学习.
根据我的阅读,以下代码应该工作.但是,只有服务器似乎正常工作.客户端发送消息,但不会收到服务器的回复.
感谢您提供任何帮助.
服务器:
-- Server
#!/usr/bin/env lua5.1
local socket = require("socket")
udp = socket.udp()
udp:setsockname("*", 53474)
udp:settimeout(0)
while true do
data, ip, port = udp:receivefrom()
if data then
print("Received: ", data, ip, port)
udp:sendto(data, ip, port)
end
socket.sleep(0.01)
end
Run Code Online (Sandbox Code Playgroud)
客户:
-- Client
#!/usr/bin/env lua5.1
local socket = require("socket")
udp = socket.udp()
udp:setpeername("127.0.0.1", 53474)
udp:settimeout(0)
udp:send("Data!")
data = udp:receive()
if data then
print("Received: ", data)
end
Run Code Online (Sandbox Code Playgroud)