#!/usr/bin/ruby
#server
require 'socket'
s = UDPSocket::new
s.bind('', 54321)
connected_client = Array::new
while true
packet = s.recvfrom(255)
if packet[0] == 'init'
new_num_client = rand(1000)
s.send(new_num_client.to_s, 0, packet[1][3], packet[1][1])
connected_client.push(new_num_client)
Thread::new(s, packet[0], packet[1][3], packet[1][1]){|s, data, ip, port|
puts "A new client connected"
while true
sleep(1.0)
s.send("server data", 0, ip, port)
end
}
else
data = packet[0].split('|')
puts "according to data #{data[1]} from client #{data[1]} change some state of the server"
end
end
|