----------------------------------------------------------------------------- -- TCP sample: Little program to dump lines received at a given port -- LuaSocket sample files -- Author: Diego Nehab, modifield by Doug Rice -- RCS ID: $Id: listener.lua,v 1.11 2005/01/02 22:44:00 diego Exp $ ----------------------------------------------------------------------------- -- http://w3.impa.br/~diego/software/luasocket/ -- https://github.com/diegonehab/luasocket -- -- ----------------------------------------------------------------------------- local socket = require("socket") host = host or "*" port = port or 9999 if arg then host = arg[1] or host port = arg[2] or port end print("Binding to host '" ..host.. "' and port " ..port.. "...") s = assert(socket.bind(host, port)) i, p = s:getsockname() assert(i, p) print("s:getsockname()", i, p) cnt = 0 while cnt < 10 do print("Waiting connection "..cnt.." from talker on " .. i .. ":" .. p .. "...") c = assert(s:accept()) print("Connected. Here is the stuff:") c:send("Hello..\n\r") as, ps = s:getsockname() ac, pc = c:getpeername() opStr = "[=]connection: ".. as .. ":" .. ps .." from " .. ac .. ":" .. pc .. " \n\r" c:send( opStr ) print( opStr ) l, e = c:receive() print( "[1]", e, l ) while not e do --print( e, l ) c:send("ack\n\r") l, e = c:receive() print(e,l) if( l == "go" ) then c:send("[=]started.... \n\r") print(e,"[=]started.... ") end if( l == "stop" ) then c:send("[=]stop... \n\r") print(e,"[=]stop... ") end if( l == "?" ) then c:send("[=]?: \n\r") as, ps = s:getsockname() ac, pc = c:getpeername() opStr = "[=]S: ".. as .. ":" .. ps .." from " .. ac .. ":" .. pc .. " \n\r" c:send( opStr ) print(e, "[=]?... ", c:getpeername() , opStr ) -- print(e, "[=]?... ", S:getpeername() ) -- print(e, "[=]?... ", S:getsockname() ) end if( l == "" ) then c:send("commands: go,stop,?,\n\r") print(e,"[=]stop... ") end end print(e) cnt = cnt + 1 end