require 'socket'
HOST = 'localhost'
PORT = 4505
server = TCPServer.new(HOST, PORT)
# array to store all the active connections
sessions = []
while (session = server.accept)
# push the current session(socket) in the array
sessions << session
# initialize a new thead for each connection
Thread.new(session) do |local_session|
# each time a client sends some data send it to all the connections
while(true)
data = local_session.gets
sessions.each do |s|
begin
s.puts data
rescue Errno::ECONNRESET
# an exception is raised, that means the connection to the client is broken
sessions.delete(s)
end
end
end
end
end