2013-04-10 32 views
0

我想了解Socket类和我使用下面的例子来实现服务器样本如何远程登录地址?

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例如地址本地主机:通过LUA 8080 ?

编辑: 我忘了说什么,我甚至不能在cmd上telnet。当我键入命令:

的telnet IP端口

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

回答

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() 
2

首先,按照说明从here将启动telnet在Windows 7:

  1. 进入控制面板
  2. 查找ProgramsTurn Windows features on or off(取决于布局)
  3. 查找Telnet client并启用它。

一旦你这样做了,它应该按预期工作。

+0

我这样做了。实际上,当我输入telnet命令,它连接,但当我发送一条消息(例如“你好”),我的连接丢失,我的lua程序无法收到消息。我不知道为什么... – Crasher 2013-04-10 14:57:31

+0

Telnet需要客户端和服务器。 – Zyerah 2013-04-10 16:02:59

+0

我的客户端和服务器处于活动状态,但仍然收到相同的消息,不知道为什么... – Crasher 2013-04-10 16:50:47