Advertisement
amigojapan

BlindBBS

May 6th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.92 KB | None | 0 0
  1. -- load namespace
  2. local socket = require("socket")
  3. local server = socket.tcp()
  4. server:bind('*',5002) -- bind on port 5000
  5. server:listen(32)
  6. server:settimeout(0.01)--make it non blocking, making this value smaller makes it faster, but probably mroe CPU load, and 0 does not work(i think)
  7.  
  8. -- find out which port the OS chose for us
  9. local ip, port = server:getsockname()
  10. -- print a message informing what's up
  11. print("Please connect to localhost on port " .. port)
  12. print (ip)
  13.  
  14. clients = {}
  15. while 1 do
  16.     local c,s  = server:accept()
  17.     --print(s)
  18.     if s == nil then -- not timeout
  19.         table.insert(clients, c)
  20.         c:settimeout(0)
  21.         print("client connected...")
  22.     end
  23.     for key,client in pairs(clients) do
  24.         -- receive the line
  25.         local line, err = client:receive()
  26.         if not err then print (key .. ":" .. line) end
  27.         -- if there was no error, send it back to the client
  28.         if not err then client:send(line .. "\n") end
  29.     end
  30. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement