Sockets are not tables anymore.

This commit is contained in:
Diego Nehab 2002-07-08 20:56:36 +00:00
parent c27087ca95
commit 112e51be58

View File

@ -20,49 +20,52 @@ if arg then
port2 = arg[3] or port2 port2 = arg[3] or port2
end end
server1 = bind(host, port1) server1, error = bind(host, port1)
if not server1 then print(error) exit() end
server1:timeout(1) server1:timeout(1)
server1.is_server = 1 server2, error = bind(host, port2)
server2 = bind(host, port2) if not server2 then print(error) exit() end
server2:timeout(1) server2:timeout(1)
server2.is_server = 1
set = {server1, server2} sock_set = {server1, server2}
number = 1
sock_id = {}
sock_id[server1] = 1
sock_id[server2] = 2
next_id = 3
while 1 do while 1 do
local r, s, e, l, n local readable, _, error = select(sock_set, nil)
r, _, e = select(set, nil) for _, sock in readable do
for i, v in r do -- is it a server socket
if v.is_server then if sock_id[sock] < 3 then
s = v:accept() local incomming = sock:accept()
if s then if incomming then
s:timeout(1) incomming:timeout(1)
s.number = number sock_id[incomming] = next_id
number = number + 1 set_add(sock_set, incomming)
set_add(set, s) write("Added client id ", next_id, ". ",
write("Added client number ", s.number, ". ", getn(sock_set)-2, " total.\n")
getn(set)-2, " total.\n") next_id = next_id + 1
end end
-- it is a client socket
else else
l, e = v:receive() local line, error = sock:receive()
n = v.number local id = sock_id[sock]
if e then if error then
v:close() sock:close()
set_remove(set, v) set_remove(sock_set, sock)
write("Removed client number ", n, ". ", write("Removed client number ", id, ". ",
getn(set)-2, " total.\n") getn(sock_set)-2, " total.\n")
else else
write("Broadcasting line '", tostring(n), "> ", write("Broadcasting line '", id, "> ", line, "'.\n")
tostring(l), "'.\n") _, writable, error = select(nil, sock_set, 1)
_, s, e = select(nil, set, 1) if not error then
if not e then for _, outgoing in writable do
for i,v in s do write("Sending to client ", sock_id[outgoing], "\n")
v:send(tostring(n), "> ", l, "\r\n") outgoing:send(id, "> ", line, "\r\n")
end end
else else write("No one ready to listen!!!\n") end
write("No one ready to listen!!!\n")
end
end end
end end
end end