如何telnet一个地址?

时间:2013-04-10 02:58:04

标签: lua luasocket

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

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

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

编辑: 我忘了告诉他什么,我甚至不能在cmd上telnet。当我输入命令时:

telnet ip port

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

2 个答案:

答案 0 :(得分:2)

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

  1. 转到“控制面板”
  2. Turn Windows features on or off下找到Programs(取决于布局)
  3. 查找Telnet client并启用它。
  4. 完成后,它应该按预期工作。

答案 1 :(得分: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()