New to EventMachine and Ruby so perfect storm of newbieness. So apologies in advance... I checked the docs and list archives but came up empty handed on the following: So dumb question #1. Is there a way to get the IP address & Port of a running EventMachine? Dumb question #1 part 2: can you issue a EventMachine::start_server command and have it grab the next available port similar to the way TCPServer behaves? Note, I''m not lazy just don''t know how to grab the IP & Port. N00b remember thanks in advance j -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080422/a9da0cbf/attachment.html
From: Jeff Fedor> question #1. Is there a way to get the IP address & Port of a running > EventMachine?One can obtain the IP and port of a connected TCP peer like: require ''eventmachine'' require ''socket'' interface_ip = "0.0.0.0" port = 12345 EventMachine::start_server(interface_ip, port, EventMachine::Connection) do |client_connection| remote_port, remote_ip = Socket.unpack_sockaddr_in(client_connection.get_peername) end> question #1 part 2: can you issue a EventMachine::start_server > command and have it grab the next available port similar to the way > TCPServer behaves? Note, I''m not lazy just don''t know how to grab the > IP & Port.Except in rare cases, when one starts a server, one provides a known port number that one expects clients to know about and be connecting to. The IP address provided to start_server is an indication of which interface to bind to. Generally one either specifices "0.0.0.0", meaning accept local and external connections, or "127.0.0.1", meaning accept only local connections. Hope this helps, Bill
Thanks Bill. I was hoping to be able to spark up EventMachines on the next available port on the fly but sounds like I''m an edge case appreciate the help j On Tue, Apr 22, 2008 at 8:06 PM, Bill Kelly <billk at cts.com> wrote:> > From: Jeff Fedor > > > > question #1. Is there a way to get the IP address & Port of a running > > EventMachine? > > One can obtain the IP and port of a connected TCP peer like: > > require ''eventmachine'' > require ''socket'' > > interface_ip = "0.0.0.0" > port = 12345 > > EventMachine::start_server(interface_ip, port, EventMachine::Connection) > do |client_connection| > > remote_port, remote_ip > Socket.unpack_sockaddr_in(client_connection.get_peername) > > end > > > > question #1 part 2: can you issue a EventMachine::start_server > > command and have it grab the next available port similar to the way > > TCPServer behaves? Note, I''m not lazy just don''t know how to grab the > > IP & Port. > > Except in rare cases, when one starts a server, one provides > a known port number that one expects clients to know about and > be connecting to. > > The IP address provided to start_server is an indication of which > interface to bind to. Generally one either specifices "0.0.0.0", > meaning accept local and external connections, or "127.0.0.1", > meaning accept only local connections. > > > Hope this helps, > > Bill > > > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080422/a0341564/attachment-0001.html
From: Jeff Fedor> > Thanks Bill. I was hoping to be able to spark up EventMachines on > the next available port on the fly but sounds like I''m an edge caseOK. So it sounds as though you have some sort of port-mapper in your design, such that you don''t require a TCP server to listen on a predetermined port? It appears that if you pass in zero for the port when start_server is called, the OS will choose the port for you.>From irb:>> require ''eventmachine''=> true>> EventMachine.run { EventMachine.start_server("0.0.0.0", 0, EventMachine::Connection) {|conn| p conn} }Then I used `netstat -a -p tcp` to manually look for the assigned port. (3230 the first time I ran the program, 3233 the second time.) telnet localhost 3233 did indeed confirm this was the EM server port: #<EventMachine::Connection:0x2cf6e38 @signature="d7ff9540-b4cc-4669-a4cc-901889fcf4e03"> So... it appears that it is indeed possible to allow the OS to select an ephemeral port with start_server... but I''m not sure we have a way to ask EventMachine which port was assigned. :( Regards, Bill
On 23.04.2008, at 8:17, Bill Kelly wrote:> but I''m not sure > we have a way to ask EventMachine which port was assigned.I wonder why one really needs to know its ephemeral port. When you connect to some peer it knows the remote port, so there''s no reason to send a message about a port anywhere. (As previously mentioned: port, ip Socket.unpack_sockaddr_in(conn.get_peername))
From: "Oleg Andreev" <oleganza at gmail.com>> > On 23.04.2008, at 8:17, Bill Kelly wrote: > >> but I''m not sure >> we have a way to ask EventMachine which port was assigned. > > I wonder why one really needs to know its ephemeral port. When you > connect to some peer it knows the remote port, so there''s no reason to > send a message about a port anywhere. > > (As previously mentioned: port, ip > Socket.unpack_sockaddr_in(conn.get_peername))My understanding was Jeff wanted to start a *server* whose listen port would be ephemeral. An example would be an FTP client creating an ''active'' data connection socket (or an FTP server creating a ''passive'' socket.) An FTP client initiating an ''active'' data transfer, creates a TCP server on an ephemeral port, then uses (for example) getsockname + getnameinfo to obtain the ephemeral port number, and passes that port number over the control connection to the server, which then actively connects to the TCP server on that ephemeral port just provided by the client. Since EM does allow a TCP server to be created with an ephemeral port via start_server, it does seem like it would be nice to be able to determine that port number. :) Regards, Bill
Yep that''s it and yep would love to be able to determine the port :-) these new people are trouble makers huh?!?! j On Wed, Apr 23, 2008 at 2:49 AM, Bill Kelly <billk at cts.com> wrote:> > From: "Oleg Andreev" <oleganza at gmail.com> > > > > On 23.04.2008, at 8:17, Bill Kelly wrote: > > > >> but I''m not sure > >> we have a way to ask EventMachine which port was assigned. > > > > I wonder why one really needs to know its ephemeral port. When you > > connect to some peer it knows the remote port, so there''s no reason to > > send a message about a port anywhere. > > > > (As previously mentioned: port, ip > > Socket.unpack_sockaddr_in(conn.get_peername)) > > My understanding was Jeff wanted to start a *server* whose > listen port would be ephemeral. > > An example would be an FTP client creating an ''active'' > data connection socket (or an FTP server creating a ''passive'' > socket.) > > An FTP client initiating an ''active'' data transfer, creates > a TCP server on an ephemeral port, then uses (for example) > getsockname + getnameinfo to obtain the ephemeral port number, > and passes that port number over the control connection to > the server, which then actively connects to the TCP server on > that ephemeral port just provided by the client. > > Since EM does allow a TCP server to be created with an > ephemeral port via start_server, it does seem like it would be > nice to be able to determine that port number. :) > > > Regards, > > Bill > > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080423/c691b772/attachment.html