0

我正在尝试了解套接字类,并且我正在使用以下示例来实现服务器示例

local server = assert(socket.bind("*", 0))

-- find out which port the OS chose for us
local ip, port = server:getsockname()

-- print a message informing what's up
print("Please telnet to localhost on IP [" ..ip.. "] and port [" .. port .. "]")
print("After connecting, you have 10s to enter a line to be echoed")

-- loop forever waiting for clients
while true do
-- wait for a connection from any client
local client = server:accept()

-- make sure we don't block waiting for this client's line
client:settimeout(10)

-- receive the line
local line, err = client:receive()

-- if there was no error, send it back to the client
if not err then
    client:send(line .. "\n")
end

-- done with client, close the object
client:close()
end

但现在的问题是,我如何通过 lua 远程登录例如地址 localhost:8080?

编辑:我忘了说点什么,我什至不能在 cmd 上远程登录。当我键入命令时:

远程登录 ip 端口

我发送消息后它总是说“连接丢失”。我究竟做错了什么?

4

2 回答 2

2

首先,按照此处的说明在 Windows 7 中启用 telnet:

  1. 转到控制面板
  2. Turn Windows features on or off在下查找Programs(取决于布局)
  3. 找到Telnet client并启用它。

完成此操作后,它应该可以按预期工作。

于 2013-04-10T05:15:10.033 回答
1

完毕!

local socket = require("socket")

local server = socket.connect(ip, port)

local ok, err = server:send("RETURN\n")
if (err ~= nil) then
    print (err)
else
    while true do
        s, status, partial = server:receive(1024)
        print(s or partial)

        if (status == "closed") then
            break
        end
    end
end

server:close()
于 2013-04-14T21:31:53.690 回答