T
Tim Uckun
[Note: parts of this message were removed to make it a legal post.]
I am starting to write a pretty simple UDP server using eventmachine. So
far my code consists of a server which sleeps for a period of time and then
echoes back what it got sent and a threaded client which sends a thousand
udp packets consisting of the integer and prints the replies it gets.
The problem is that the server only processes 361 requests. The requests are
processed in order (which is expected I guess) but clearly the rest of the
udp messages are being lost to the ether.
I have included my server and client below and if somebody could explain why
this is happening and how I can fix it I would appreciate it.
BTW is eventmachine still in development? Looks like everything on the web
site is from 2006 and the mailing list doesn't work.
-------Code--------
-----Client--------
require 'socket'
UDP_RECV_TIMEOUT = 3
server_port = 7779
server_addr = '127.0.0.1'
def send_udp_message(msg, server_addr, server_port, respond = false)
begin
sock = UDPSocket.open
sock.send(msg, 0, server_addr, server_port)
if respond
message, status = sock.recvfrom(65536)
return message
end
rescue IOError, SystemCallError
ensure
sock.close if sock
end
end
threads = []
1000.times do |i|
threads << Thread.new do
result = send_udp_message i.to_s, server_addr, server_port, true
puts "Sent #{i} got #{result}"
end
end
threads.each {|t| t.join }
----------- Server ---------------
require 'eventmachine'
module UmmpServer
def post_init
puts "client connected!"
end
def receive_data(data)
sleep_time = rand(5)
message = "#{data} -- #{sleep_time}"
puts "Sleeping for #{sleep_time}"
sleep sleep_time
p message
send_data(message) #Eventmachine will make this return-to-sender
by default
end
end
class Echo < EventMachine::Connection
def initialize(*args)
puts "starting server"
super
# stuff here...
end
def receive_data(data)
# resp = EventMachine:
p data
send_data data
close_connection_after_writing
end
def unbind
p ' connection totally closed'
end
end
EventMachine::run do
EventMachine.epoll
EventMachine:pen_datagram_socket('0.0.0.0', 7779, UmmpServer)
EventMachine::add_periodic_timer(1) {
EventMachine::stop_event_loop unless $running
}
EventMachine::add_periodic_timer( 10 ) { $stderr.write "*" }
puts "Now accepting connections on address , port #7779..."
end
puts "All done for that....."
I am starting to write a pretty simple UDP server using eventmachine. So
far my code consists of a server which sleeps for a period of time and then
echoes back what it got sent and a threaded client which sends a thousand
udp packets consisting of the integer and prints the replies it gets.
The problem is that the server only processes 361 requests. The requests are
processed in order (which is expected I guess) but clearly the rest of the
udp messages are being lost to the ether.
I have included my server and client below and if somebody could explain why
this is happening and how I can fix it I would appreciate it.
BTW is eventmachine still in development? Looks like everything on the web
site is from 2006 and the mailing list doesn't work.
-------Code--------
-----Client--------
require 'socket'
UDP_RECV_TIMEOUT = 3
server_port = 7779
server_addr = '127.0.0.1'
def send_udp_message(msg, server_addr, server_port, respond = false)
begin
sock = UDPSocket.open
sock.send(msg, 0, server_addr, server_port)
if respond
message, status = sock.recvfrom(65536)
return message
end
rescue IOError, SystemCallError
ensure
sock.close if sock
end
end
threads = []
1000.times do |i|
threads << Thread.new do
result = send_udp_message i.to_s, server_addr, server_port, true
puts "Sent #{i} got #{result}"
end
end
threads.each {|t| t.join }
----------- Server ---------------
require 'eventmachine'
module UmmpServer
def post_init
puts "client connected!"
end
def receive_data(data)
sleep_time = rand(5)
message = "#{data} -- #{sleep_time}"
puts "Sleeping for #{sleep_time}"
sleep sleep_time
p message
send_data(message) #Eventmachine will make this return-to-sender
by default
end
end
class Echo < EventMachine::Connection
def initialize(*args)
puts "starting server"
super
# stuff here...
end
def receive_data(data)
# resp = EventMachine:
p data
send_data data
close_connection_after_writing
end
def unbind
p ' connection totally closed'
end
end
EventMachine::run do
EventMachine.epoll
EventMachine:pen_datagram_socket('0.0.0.0', 7779, UmmpServer)
EventMachine::add_periodic_timer(1) {
EventMachine::stop_event_loop unless $running
}
EventMachine::add_periodic_timer( 10 ) { $stderr.write "*" }
puts "Now accepting connections on address , port #7779..."
end
puts "All done for that....."