2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
31
Task/Echo-server/Lua/echo-server.lua
Normal file
31
Task/Echo-server/Lua/echo-server.lua
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
require("socket")
|
||||
|
||||
function checkOn (client)
|
||||
local line, err = client:receive()
|
||||
if line then
|
||||
print(tostring(client) .. " said " .. line)
|
||||
client:send(line .. "\n")
|
||||
end
|
||||
if err and err ~= "timeout" then
|
||||
print(tostring(client) .. " " .. err)
|
||||
client:close()
|
||||
return err
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local delay, clients, newClient = 10^-6, {}
|
||||
local server = assert(socket.bind("*", 12321))
|
||||
server:settimeout(delay)
|
||||
print("Server started")
|
||||
while 1 do
|
||||
repeat
|
||||
newClient = server:accept()
|
||||
for k, v in pairs(clients) do
|
||||
if checkOn(v) then table.remove(clients, k) end
|
||||
end
|
||||
until newClient
|
||||
newClient:settimeout(delay)
|
||||
print(tostring(newClient) .. " connected")
|
||||
table.insert(clients, newClient)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue