Justin Collins
5/15/2009 11:08:00 PM
Diego Bernardes wrote:
> I need a dynamic port forwarding application, i listem packets from a ip
> on port 10000, then i need not just send the data i receive to other
> ports, but i need first create a connection on this new port first and
> listen to any connection on this port, if i get any thing on this port i
> send back through the first connection.
>
> Its easy to create a single server with EventMachine like the code
> below:
>
> require 'rubygems'
> require 'eventmachine'
>
> module Echo
> def receive_data data
> send_data data
> end
> end
>
> EM.run {
> EM.start_server "0.0.0.0", 10000, Echo
> }
>
>
> But how could i create another server inside the current server?
>
Well, you can start as many servers as you would like, but it sounds
more like you need to open a connection to a server, not start another.
You can use EventMachine::connect for that.
I believe you can do something like (completely untested):
require 'rubygems'
require 'eventmachine'
module Other
attr_accessor :sender
def receive_data data
self.sender.send_data data
end
end
module Echo
def post_init
@other_side = EM.connect "0.0.0.0", 10001, Other
@other_side.sender = self
end
def receive_data data
@other_side.send_data data
end
end
EM.run {
EM.start_server "0.0.0.0", 10000, Echo
}
-Justin