Skip to content

Instantly share code, notes, and snippets.

@whiler
Created January 4, 2017 10:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save whiler/35289e6e9b4239708dce72a591d295ec to your computer and use it in GitHub Desktop.
Save whiler/35289e6e9b4239708dce72a591d295ec to your computer and use it in GitHub Desktop.
Simple TCP Echo Server in Lua
local signal = require("posix.signal")
local socket = require("socket")
local string = require("string")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("127.0.0.1", 0))
local ip, port = server:getsockname()
print(string.format("telnet %s %s", ip, port))
local running = 1
local function stop(sig)
running = 0
return 0
end
-- Interrupt
signal.signal(signal.SIGINT, stop)
while 1 == running do
local client = server:accept()
client:settimeout(9)
local msg, err = client:receive()
while not err and "quit" ~= msg do
print(string.format("received: %s", msg))
client:send(msg)
client:send("\n")
msg, err = client:receive()
end
client:close()
end
server:close()
@fl215
Copy link

fl215 commented Apr 9, 2024

codes like yoda

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment