# server.rb

require 'socket'

port = 3456              # ポート番号
clients = 2              # 必要クライアント数

ss = TCPServer.open(port)
print "waiting for ",clients," clients\n"
csocks = []
clients.times{
  csocks << ss.accept
}
puts "all accepted"
while csocks != [] do
  inputs = IO.select(csocks)
  if inputs != nil 
    cs = inputs[0][0]
    if cs.eof?
      csocks -= [cs]
      cs.close
    else
      msg = cs.gets.chomp
      csocks.each{|csi|
	   csi.puts msg  # すべてに送ってよい
      }
    end
  end
end
ss.close