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 .. "\n") end 为
if not err then client:send("received: '" line .. "'.\n") 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\receive。
- 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 lib\Socket.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
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
- 参考:
- LuaSocket: Network support for the Lua language(貌似版本过期),
- Lua TCP/IP simple Client Server connection - Stack Overflow,l
- ua - LuaSocket server:accept() timeout (TCP) - Stack Overflow,
- Lua sockets - Asynchronous Events - Stack Overflow,
- lua socket client server - Google 搜索,
- Luasocket 服务器,客户端简单实例_shower_wind的专栏-CSDN博客
- 实例解析lua中的socket(TCP,UDP)_wxywxywxy110的博客-CSDN博客_lua tcp
tcp:listen() 。
测试例用法:
- telnet连接后,发送 GET / 回车, a:b ..回车;发送 GET /mouse 回车;..
- 游览器打开地址http://localhost:1337/(可以多开)。游览器会向服务端请求信息,服务端返回。
- 可以修改 setTimeout ,配合 OutputDebug, % Line 查看游览器向服务端请求信息。
修改版,增加timeout,参见iPhilip的回帖。
Android Simple TCP Socket Tester
post of geek use cache (queue) to minimize/prevent block.
OnAccept based OnMessage in Critical mode (of ahk).
参见AutoHotkey Help\OnMessage\General Remarks (搜 critical),或可使用EventProcRegister-OnMessage(MaxThreads)实现多连接。
(类似OnAccept)另有OnRecv功能未知。
critical和MaxThreads不能同时存在,前者优先。
- 127.0.0.1和0.0.0.0地址的区别(及localhost)(推荐):IP地址表示(net-id、host-id)、IP地址分类(A~E)、特殊IP地址、..
- Bind as client, could receive Read/Accept/Close message from server; Connect as server, could receive Read/Close from client
自socket.ahk\EventProcRegister。
accept后会生成socket()重新注册onMessage。(最开始为SocketTCP) - 不适用Critical的话,是FILO,会打乱顺序..
如sleep 1-sleep 5>5-1。