Previously, I showed code for writing an echo client and server in Ruby. For fun, here's an implementation in Lua. The client and server classes are both provided in the Lua module echo.lua.
echo.lua
-- module setup
local modname = ...
local M = {}
_G[modname] = M
package.loaded[modname] = M
-- import section
local assert = assert
local setmetatable = setmetatable
local socket = require 'socket'
local string = string
-- no more external access beyond this point
setfenv(1, M)
-- private functions
local function add_lf(s)
if string.sub(s, #s, #s) ~= '\n' then
return s .. '\n'
else
return s
end
end
-- echo server
EchoServer = {}
function EchoServer:serve_forever()
local sock = socket.tcp()
assert(sock:bind(self.host, self.port))
sock:listen(5)
while true do
local client = sock:accept()
local line = client:receive('*line')
if line then
client:send(add_lf(line))
end
client:close()
end
end
function EchoServer:new(host, port)
local o = {}
o.host = host or '127.0.0.1'
o.port = port or 8765
setmetatable(o, self)
self.__index = self
return o
end
-- echo client
EchoClient = {}
function EchoClient:request(msg)
local sock = socket.tcp()
assert(sock:connect(self.host, self.port))
sock:send(add_lf(msg))
local response = sock:receive('*line')
sock:close()
return response
end
function EchoClient:new(host, port)
local o = {}
o.host = host or '127.0.0.1'
o.port = port or 8765
setmetatable(o, self)
self.__index = self
return o
end
Creating a script to run the echo server is quite simple:
echo_server_example.lua
local echo = require 'echo' local server = echo.EchoServer:new() server:serve_forever()
Likewise, the script for the echo client is also straight-forward:
echo_client_example.lua
local echo = require 'echo'
local client = echo.EchoClient:new()
print(client:request('hello'))
print(client:request('goodbye'))
echo_client_example.lua output
hello goodbye
No comments:
Post a Comment