2003-06-09 20:23:40 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Select sample: simple text line server
|
|
|
|
-- LuaSocket 1.5 sample files.
|
|
|
|
-- Author: Diego Nehab
|
|
|
|
-- RCS ID: $Id$
|
|
|
|
-----------------------------------------------------------------------------
|
2001-03-12 20:42:46 +01:00
|
|
|
host = host or "*"
|
|
|
|
port1 = port1 or 8080
|
|
|
|
port2 = port2 or 8081
|
|
|
|
if arg then
|
|
|
|
host = arg[1] or host
|
|
|
|
port1 = arg[2] or port1
|
|
|
|
port2 = arg[3] or port2
|
|
|
|
end
|
|
|
|
|
2003-03-28 22:24:30 +01:00
|
|
|
server1, error = socket.bind(host, port1)
|
2003-06-09 20:23:40 +02:00
|
|
|
assert(server1, error)
|
2001-03-12 20:42:46 +01:00
|
|
|
server1:timeout(1)
|
2003-03-28 22:24:30 +01:00
|
|
|
server2, error = socket.bind(host, port2)
|
2003-06-09 20:23:40 +02:00
|
|
|
assert(server2, error)
|
2001-03-12 20:42:46 +01:00
|
|
|
server2:timeout(1)
|
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
function newset()
|
|
|
|
local reverse = {}
|
|
|
|
local set = {}
|
|
|
|
setmetatable(set, { __index = {
|
|
|
|
insert = function(set, value)
|
|
|
|
table.insert(set, value)
|
|
|
|
reverse[value] = table.getn(set)
|
|
|
|
end,
|
|
|
|
remove = function(set, value)
|
|
|
|
table.remove(set, reverse[value])
|
|
|
|
reverse[value] = nil
|
|
|
|
end,
|
|
|
|
id = function(set, value)
|
|
|
|
return reverse[value]
|
|
|
|
end
|
|
|
|
}})
|
|
|
|
return set
|
|
|
|
end
|
|
|
|
|
|
|
|
sockets = newset()
|
2002-07-08 22:56:36 +02:00
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
sockets:insert(server1)
|
|
|
|
sockets:insert(server2)
|
2001-03-12 20:42:46 +01:00
|
|
|
|
|
|
|
while 1 do
|
2003-06-09 20:23:40 +02:00
|
|
|
local readable, _, error = socket.select(sockets, nil)
|
|
|
|
for _, input in readable do
|
|
|
|
-- is it a server socket?
|
|
|
|
local id = sockets:id(input)
|
|
|
|
if input == server1 or input == server2 then
|
|
|
|
local new = input:accept()
|
|
|
|
if new then
|
|
|
|
new:timeout(1)
|
|
|
|
sockets:insert(new)
|
|
|
|
io.write("Server ", id, " got client ", sockets:id(new), "\n")
|
2001-03-12 20:42:46 +01:00
|
|
|
end
|
2002-07-08 22:56:36 +02:00
|
|
|
-- it is a client socket
|
2001-03-12 20:42:46 +01:00
|
|
|
else
|
2003-06-09 20:23:40 +02:00
|
|
|
local line, error = input:receive()
|
2002-07-08 22:56:36 +02:00
|
|
|
if error then
|
2003-06-09 20:23:40 +02:00
|
|
|
input:close()
|
|
|
|
io.write("Removing client ", id, "\n")
|
|
|
|
sockets:remove(input)
|
2001-03-12 20:42:46 +01:00
|
|
|
else
|
2003-03-28 22:24:30 +01:00
|
|
|
io.write("Broadcasting line '", id, "> ", line, "'.\n")
|
2003-06-09 20:23:40 +02:00
|
|
|
__, writable, error = socket.select(nil, sockets, 1)
|
2002-07-08 22:56:36 +02:00
|
|
|
if not error then
|
2003-06-09 20:23:40 +02:00
|
|
|
for ___, output in writable do
|
|
|
|
io.write("Sending to client ", sockets:id(output), "\n")
|
|
|
|
output:send(id, "> ", line, "\r\n")
|
2001-03-12 21:00:47 +01:00
|
|
|
end
|
2003-03-28 22:24:30 +01:00
|
|
|
else io.write("No one ready to listen!!!\n") end
|
2001-03-12 21:00:47 +01:00
|
|
|
end
|
2001-03-12 20:42:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|