Created
January 4, 2017 10:14
-
-
Save whiler/35289e6e9b4239708dce72a591d295ec to your computer and use it in GitHub Desktop.
Simple TCP Echo Server in Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
codes like yoda