Skip to content

Instantly share code, notes, and snippets.

@epitron
Created December 14, 2009 10:43
Show Gist options
  • Save epitron/255965 to your computer and use it in GitHub Desktop.
Save epitron/255965 to your computer and use it in GitHub Desktop.
#---------------------------------------------------------------------------
# The first one receives and prints a datagram:
#---------------------------------------------------------------------------
require 'rubygems'
require 'eventmachine'
module A
def receive_data a
p a
end
end
EM.run {
p "Listening on 9600"
EM.open_datagram_socket "localhost", 9600, A
}
#---------------------------------------------------------------------------
# The third one sends a datagram to start off the process:
#---------------------------------------------------------------------------
require 'rubygems'
require 'eventmachine'
module G
def post_init
send_datagram "AAA", "localhost", 9601
end
end
EM.run {
EM.open_datagram_socket "localhost", 0, G
}
#---------------------------------------------------------------------------
# The second one waits to receive a datagram, then sends it to a different
# port.
#---------------------------------------------------------------------------
require 'rubygems'
require 'eventmachine'
module A
def receive_data a
p a
send_datagram "<#{a}>", "localhost", 9600
end
end
EM.run {
p "Listening on 9601, sending to 9600"
EM.open_datagram_socket "localhost", 9601, A
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment