Lua/AHK socket tcp telnet

  • 库名为socket(而非luasocket)。
  • 当前版本( require("socket")._VERSION )为3.0-rc1。 
  • master+bind-listen local address=service

master+connect remote address=client

  • service.accept/getsockname/setoption/settimeout/close

client.send/receive/getsockname/getpeername/setoption/settimeout/shutdown/close

  • in server, client=server:accept()

then  client:send/receive 

  • in client, only has concept of tcp(master) (no server/client), and host+port (of server).

then  tcp:send/receive 

  • 安装telnet。win+q搜telnet,“windows功能”对话框列表中找到并勾选即可安装。
  • 使用telnet。 
    • o localhost <port> (不可使用0.0.0.0)建立连接;
    • 建立连接后, sen ... ,但总是“遗失对主机的连接。”输入的内容直接被发送到服务端(无需 sen 命令)。
    • 建议更改Luasocket测试例 if not err then client:send(line .. " ") end 为
      if not err then
          client:send("received: '" line .. "'.
      ")
      end

      否则telnet中收到的消息如

sen abc


遗失对主机的连接。

按任意键继续...

以为失败了呢...其实是server中 client:send(...) 后 client:close() 。

  • server/client都可以close,对方(client/server)会收到(receive)状态(status/error message) s, status/err, partial = tcp:receive() 值为"close"。参见reference eceive
  • autohotkey(AHK) tcp:见下链接。
    • CONNECTED/ACCEPTED event is fired on both client and server
    • the server/client can both receives the DISCONNECT event
  • 建立TCP连接需要时间。
  • autohotkey lua socket TCP 通讯 例子
#NoEnv
#SingleInstance,Force
#Include libSocket.ahk
client := new SocketTCP()
return
F1::
    try{
        if(not connect){
            connect:=true
            ToolTip, my Socke`nConnecting..
            client.Connect(["localhost", 8000])
            ;    or 127.0.0.0
        }
        tickCount:=A_TickCount
        ToolTip, my Socke`nSend %tickCount%
        client.SendText(tickCount (Mod(tickCount,2)==1 ? "`n" : ","))
    }catch e{
        ToolTip, my Socket`nConnent/Send/Disconnect Failed
    }
    return
F2::
OnExit:
    client.Disconnect()
    connect:=false
    ToolTip
    return
#IfWinActive ahk_exe SciTE.exe
F1::
    Send {F5}
    return
F2::ExitApp
View Code
local socket=require("socket")
print(socket._VERSION)--LuaSocket 3.0-rc1
--my Socket TCP server
local server = assert(socket.bind("0.0.0.0", 8000))
print'server ready.'
local client,status = server:accept()
if client then
    print'client ready.'
    local received, status,partial
    repeat
        received, status,partial = client:receive'*l'
        if status then
            print('receive err: ',status,', partial: ',partial)
            if status=='closed' then
                client=nil
            end
        else
            print('received: ',received)
        end
    until status
    print'client closed.'
else
    print('accept client error, status: ',status)
end
View Code

 tcp:listen() 。

测试例用法:

  • telnet连接后,发送 GET / 回车, a:b ..回车;发送 GET /mouse 回车;..
  • 游览器打开地址http://localhost:1337/(可以多开)。游览器会向服务端请求信息,服务端返回。
  • 可以修改 setTimeout ,配合 OutputDebug, % Line 查看游览器向服务端请求信息。

修改版,增加timeout,参见iPhilip的回帖

Android Simple TCP Socket Tester

原文地址:https://www.cnblogs.com/RobertL/p/14193923.html