----------------------------------------------------------------------------- -- TCP sample: Little program to dump lines received at a given port -- LuaSocket sample files -- Author: Diego Nehab -- RCS ID: $Id: listener.lua,v 1.11 2005/01/02 22:44:00 diego Exp $ ----------------------------------------------------------------------------- local socket = require("socket") host = host or "*" port = port or 8080 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("Waiting connection from talker on " .. i .. ":" .. p .. "...") c = assert(s:accept()) print("Connected. Here is the stuff:") l, e = c:receive() while not e do print("|"..l) l, e = c:receive() end print(e) l = io.read() ---8<---------------------------------------------- ----------------------------------------------------------------------------- -- TCP sample: Little program to send text lines to a given host/port -- LuaSocket sample files -- Author: Diego Nehab -- RCS ID: $Id: talker.lua,v 1.9 2005/01/02 22:44:00 diego Exp $ ----------------------------------------------------------------------------- local socket = require("socket") host = host or "localhost" port = port or 8080 if arg then host = arg[1] or host port = arg[2] or port end print("Attempting connection to host '" ..host.. "' and port " ..port.. "...") c = assert(socket.connect(host, port)) c = assert(socket.connect(host, port)) c = assert(socket.connect(host, port)) c = assert(socket.connect(host, port)) print("Connected! Please type stuff (empty line to stop):") l = io.read() while l and l ~= "" and not e do assert(c:send(l .. "\n")) l = io.read() end -- l = io.read() ---8<---------------------------------------------- Microsoft Windows [Version 10.0.22000.556] (c) Microsoft Corporation. All rights reserved. C:\Users\dough>netstat -n Active Connections Proto Local Address Foreign Address State TCP 127.0.0.1:8080 127.0.0.1:54734 ESTABLISHED TCP 127.0.0.1:8080 127.0.0.1:54735 ESTABLISHED TCP 127.0.0.1:8080 127.0.0.1:54736 ESTABLISHED TCP 127.0.0.1:8080 127.0.0.1:54737 ESTABLISHED TCP 127.0.0.1:54734 127.0.0.1:8080 ESTABLISHED TCP 127.0.0.1:54735 127.0.0.1:8080 ESTABLISHED TCP 127.0.0.1:54736 127.0.0.1:8080 ESTABLISHED TCP 127.0.0.1:54737 127.0.0.1:8080 ESTABLISHED ---8<---------------------------------------------- Multipls connections that have been orphaned. Binding to host '*' and port 8080... Waiting connection from talker on 0.0.0.0:8080... Connected. Here is the stuff: closed ---8<---------------------------------------------- C:\Users\dough>netstat -n Active Connections Proto Local Address Foreign Address State TCP 127.0.0.1:8080 127.0.0.1:54750 ESTABLISHED TCP 127.0.0.1:54750 127.0.0.1:8080 ESTABLISHED Binding to host '*' and port 8080... Waiting connection from talker on 0.0.0.0:8080... Connected. Here is the stuff: |hello |doug |how |are |you? Attempting connection to host 'localhost' and port 8080... Connected! Please type stuff (empty line to stop): hello doug how are you?