# Eventmachine questions?

**URL:** <https://www.ruby-forum.com/t/eventmachine-questions/157425>\
**Category:** Ruby\
**Created:** [January 11, 2009, 5:24am UTC](https://www.ruby-forum.com/t/eventmachine-questions/157425 "2009-01-11T05:24:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [January 11, 2009, 5:24am UTC](https://www.ruby-forum.com/t/eventmachine-questions/157425/1 "2009-01-11T05:24:27Z")

</div>

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::open\_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…”

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [January 11, 2009, 11:43pm UTC](https://www.ruby-forum.com/t/eventmachine-questions/157425/2 "2009-01-11T23:43:02Z")

</div>

On Jan 10, 2009, at 10:22 PM, Tim U. wrote:

> 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.

Tim,

the mailing list is alive and well. Most of the action these days is  
on IRC so check out the #eventmachine forum on [irc.freenode.net](http://irc.freenode.net).

The new home for eventmachine is [http://rubyeventmachine.com](http://rubyeventmachine.com). There is  
a pretty active community but it’s kind of like an iceberg… you only  
see a little bit of it peek above the water line.

cr
