[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

EventMachine dynamic port forwarding

Diego Bernardes

5/15/2009 10:42:00 PM

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?
--
Posted via http://www.ruby-....

7 Answers

Justin Collins

5/15/2009 11:08:00 PM

0

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

Diego Bernardes

5/17/2009 8:48:00 PM

0

Yea, i said wrong, its start a new connection, not a new server.

The example you write, run without error, but dont create the new
connection, but i got the idea, the problem with eventmachine is the
documentation =/
--
Posted via http://www.ruby-....

Diego Bernardes

5/17/2009 9:15:00 PM

0

Diego Bernardes wrote:
> Yea, i said wrong, its start a new connection, not a new server.
>
> The example you write, run without error, but dont create the new
> connection, but i got the idea, the problem with eventmachine is the
> documentation =/

Now im with a question, when i use EventMachine::connect, i connect to a
tcp port, my problem is, i need create a tcp port to others process
connect, so i think its the start server, no?
--
Posted via http://www.ruby-....

Diego Bernardes

5/19/2009 12:49:00 PM

0

Diego Bernardes wrote:
> Diego Bernardes wrote:
>> Yea, i said wrong, its start a new connection, not a new server.
>>
>> The example you write, run without error, but dont create the new
>> connection, but i got the idea, the problem with eventmachine is the
>> documentation =/
>
> Now im with a question, when i use EventMachine::connect, i connect to a
> tcp port, my problem is, i need create a tcp port to others process
> connect, so i think its the start server, no?

Someone?
--
Posted via http://www.ruby-....

Bill Kelly

5/19/2009 1:51:00 PM

0


From: "Diego Bernardes" <di3go.bernardes@gmail.com>
>
>> Now im with a question, when i use EventMachine::connect, i connect to a
>> tcp port, my problem is, i need create a tcp port to others process
>> connect, so i think its the start server, no?
>
> Someone?

http://lmgtfy.com/?q=eventmachine+st...

;)

http://rubyeventmachine.com/wiki/Co...
http://20bits.com/articles/an-eventmachine...
http://rubyeventmachine.co...
http://www.igvita.com/2008/05/27/ruby-eventmachine-the-sp...


Regards,

Bill



Roger Pack

5/19/2009 7:22:00 PM

0

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.

http://github.com/igrigorik/em-proxy/tr...
also rtunnel.
GL.
-=r
--
Posted via http://www.ruby-....

Diego Bernardes

5/21/2009 1:39:00 AM

0

I got what i need :)

the code is below:


require 'rubygems'
require 'eventmachine'

class VirtualPort < EventMachine::Connection
attr_accessor :sender

def initialize *args
super

@sender = args.first
@sender.create_client(self)
end

def receive_data data
send_data "Você digitou: " + data
puts "Cliente: " + data
@sender.send_data(data)
end
end

class Server < EventMachine::Connection
attr_accessor :client

def initialize *args
super
end

def create_client(client)
@client = client
end

def post_init
puts "Acabei de me conectar"
EM.start_server "0.0.0.0", 20000, VirtualPort, self
end

def receive_data data
send_data "Você digitou: " + data
puts "Server: " + data
if(@client)
@client.send_data(data)
end
end
end

EM.run {
EM.start_server "0.0.0.0", 10000, Server
}
--
Posted via http://www.ruby-....