# work0804.rb
# žÁ÷¥µ¡¼¥Ð

require 'socket'

ss = TCPServer.open(3456)
all = [ss]
csn = []
loop do
  inputs = IO.select(all)
  if inputs != nil 
    sock = inputs[0][0]
    if sock == ss
	cs = ss.accept
	all << cs
	csn << cs
    else
	if sock.eof?
	    csn -= [sock]
	    all -= [sock]
	    sock.close
        else
	    msg = sock.gets.chomp
	    csn.each{|csx|
		if csx != sock
		    csx.puts msg
		end
	    }
        end
    end
  end
end