【问题标题】:Lua sockets: can't get it to connect and send dataLua套接字:无法连接和发送数据
【发布时间】:2013-09-13 16:34:39
【问题描述】:

这是我第一次在 Lua 中搞乱套接字。无论我尝试什么,我都无法连接。有什么我做错或没有做的吗?

--will store room sockets
Rsock = {}
--will store pm sockets
Psock = {}
--will sore the userlist
userlist = {}
--will store our banned people list
banlist = {}
--threads table only if needed
threads = {}
sock = require "socket";
http = require "socket.http";
local iterate = 0

function getAuth(user, password)
    url = "http://old.yuribot.com/server?inp=get_auth@" 
        .. user .. "-" .. password
    return http.request(url)
end

function getServer(group)
    url = "http://old.yuribot.com/server?inp=group@".. group
    return http.request(url)
end

function room_connect(room)
    sock = sock.tcp();
    host = getServer(room);
    port = 443;
    sock.connect(host, 443);
    Rsock[room] = sock;
    sock:send("bauth:" ..  room  .. ":567765443" .. ":introbot:9911324" .. "\x00");
end

room_connect("shirayuri");
--print(getAuth("introbot", "9911324"));
--print(Rsock['shirayuri'])

P.S.:网址是shirayuri.chatango.com

【问题讨论】:

  • 可能是您的防火墙问题?我可以毫无问题地运行您的脚本(以及我的简化代码)。

标签: sockets lua send recv luasocket


【解决方案1】:

您遇到了什么错误?我认为问题在于线路 -

sock:connect(host, 443);

应该是

sock.connect(host, 443);

所以将 : 替换为 .如果您遇到的错误是

bad argument #2 to 'bind' 

如果有任何其他错误,请在此处发布。

请参阅http://www.lua.org/pil/16.html 了解冒号的作用。

【讨论】:

  • 我已经尝试了两种方式 sock.connect 不会出错,但它不会发送协议“bauth:room:uid:user:password”并且没有错误
  • 不,代码按照他分配给sock = sock.tcp()的方式工作。
  • 看这里:shirayuri.chatango.com 然后运行它,你会明白我的意思 xD
【解决方案2】:

这对我有用:

local room = 'shirayuri'
local http = require("socket.http")
local body, code = http.request("http://old.yuribot.com/server?inp=group@"..room)
if not body then error(code) end
print(body)

local socket = require("socket")
local conn = socket.tcp()
conn:connect(body, 443)
local res = conn:send("bauth:" ..  room  .. ":567765443" .. ":introbot:9911324")
print(res)

打印“s18.chatango.com”和“42”。

【讨论】:

  • 函数 room_connect(room) 是假设使用 getServer 让房间服务器连接到它并发送登录信息和登录,但不会发生遗憾
  • 您不会在脚本中的任何地方调用 getAuth 函数。
猜你喜欢
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 2020-02-29
  • 2011-07-11
  • 2011-06-12
  • 1970-01-01
  • 2013-05-29
  • 2012-02-12
相关资源
最近更新 更多