require 'socket'

def mensaje_aleatorio
	msg = ["Hola, Mundo","Melón.","Larga y próspera vida.","La Quinta Raza.","So long, and thanks for all the fish.","Spain is different.", "Com va això?","Bona tarda."]
	msg[rand(msg.length)]
end

num_hilos = 10
num_hilos.times do |i|
	Thread.start do
		j=i
		puts "Creado hilo #{i}"
		while(true)
		  puts "Abriendo socket #{j}"
			begin
				socket = TCPSocket.new("localhost",7777)
				puts "Abierto socket #{j}"
				s = mensaje_aleatorio
				socket.puts s
				puts "Enviado \"#{s}\" en #{j}"
				puts (s == (s1=socket.gets().strip())) ? "Correcto" : "Falló\nEsperado: #{s}\nDevuelto: #{s1}"
			rescue Exception => bang
				puts bang.to_s
			ensure
				socket.close unless socket.nil?
			end
		end
	end
end

while(true)
end

