require 'socket'
begin
server = TCPServer.new 2000 # Server bound to port 2000
puts "Connection Socker Created"
rescue
puts "Connection failed"
end
n = 0
def run(client, n)
puts "New Communication Thread Started"
client.puts "Hi, my new client, you are Client#{n}"
begin
while(line = client.gets)
if "Bye.".eql?(line.chomp)
client.puts "Bye bye!"
break
end
puts "#{n}: #{line}"
end
rescue IOError
puts "Error: IOError"
rescue
puts "Error... no idea what just happened"
end
client.close
end
begin
loop do
puts "Wating for Connection..."
Thread.new(run(server.accept,n))
n = n +1
end
rescue
puts "Something went wrong..."
end