Getting the IP address of the local machine

does anyone know how to get the IP address of the machine a ruby script
is running on?

-thanks

···


tom sawyer, aka transami
transami@transami.net

oh and one other thing: how to find a free port?

···

On Mon, 2002-09-09 at 03:50, Tom Sawyer wrote:

does anyone know how to get the IP address of the machine a ruby script
is running on?

-thanks


tom sawyer, aka transami
transami@transami.net


tom sawyer, aka transami
transami@transami.net

Hi,

Tom Sawyer transami@transami.net writes:

does anyone know how to get the IP address of the machine a ruby script
is running on?

require ‘socket’
ip_address = IPSocket.getaddress(Socket.gethostname)

···


eban

thanks!

···

On Mon, 2002-09-09 at 04:05, WATANABE Hirofumi wrote:

Hi,

Tom Sawyer transami@transami.net writes:

does anyone know how to get the IP address of the machine a ruby script
is running on?

require ‘socket’
ip_address = IPSocket.getaddress(Socket.gethostname)


eban


tom sawyer, aka transami
transami@transami.net

Tom Sawyer transami@transami.net writes:

oh and one other thing: how to find a free port?

you have to search a free port with trial-and-error AFAIK.
(maybe this can be done more elegant, I’m pretty new to ruby) :

[schnipp]
#!/usr/bin/env ruby

require ‘socket’

our port-range, in which we try to establish a listener

ports = 2090…2100

“define” these, to make them visible outside the iterator

server = nil
port = nil

search for port

go = false
ports.each { |port|
printf(“trying port %d\n”, port)

begin
server = TCPServer.new(‘127.0.0.1’, port)
go = true
break

rescue Errno::EADDRINUSE
# debug output
$stderr.print "listen on #{port} failed: " + $! + “\n”
end
}

listen

if (go)
$stderr.printf(“using port %d\n”, port)

while (go && (session=server.accept))

···

your server goes here

end
end
[schnapp]

regards
messju

On Mon, 2002-09-09 at 03:50, Tom Sawyer wrote:

does anyone know how to get the IP address of the machine a ruby script
is running on?

-thanks


tom sawyer, aka transami
transami@transami.net


tom sawyer, aka transami
transami@transami.net

Ouch, ouch, ouch. There’s no need to do this. Just do what you’d do in
C – bind to port 0, and let the system choose a port for you:

s = TCPServer.new(‘0’, ‘0’) # Bind to all interfaces, with any port
# > 1024.

(a quick grep through netstat shows that I got port 48944 this time).

The client classes (TCPSocket, etc.) will have a port allocated
dynamically in the same way.

Dan

···

At Tue, 10 Sep 2002 00:31:36 +0900, M Mohr wrote:

oh and one other thing: how to find a free port?

you have to search a free port with trial-and-error AFAIK.
(maybe this can be done more elegant, I’m pretty new to ruby) :


/^Dan Debertin$/
airboss@nodewarrior.org | Did I sleep a little too late,
www.nodewarrior.org | or am I awake? --Byrne

You can let the system find one for you by using 0 as a port e.g.

[mike@ratdog rubicon]$ irb
irb(main):001:0> require ‘socket’
true
irb(main):002:0> s1 = TCPServer::new(‘localhost’, 0)
#TCPServer:0x40203254
irb(main):003:0> s2 = TCPServer::new(‘localhost’, 0)
#TCPServer:0x401fb518
irb(main):004:0> s1.addr
[“AF_INET”, 54458, “localhost.localdomain”, “127.0.0.1”]
irb(main):005:0> s2.addr
[“AF_INET”, 54459, “localhost.localdomain”, “127.0.0.1”]

Hope this helps,

Mike

···

In article 873csj2o6x.fsf@pharao.lammfellpuschen.de, M Mohr wrote:

Tom Sawyer transami@transami.net writes:

oh and one other thing: how to find a free port?

you have to search a free port with trial-and-error AFAIK.
(maybe this can be done more elegant, I’m pretty new to ruby) :


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

Dan Debertin airboss@nodewarrior.org writes:

oh and one other thing: how to find a free port?

you have to search a free port with trial-and-error AFAIK.
(maybe this can be done more elegant, I’m pretty new to ruby) :

Ouch, ouch, ouch. There’s no need to do this. Just do what you’d do in
C – bind to port 0, and let the system choose a port for you:

s = TCPServer.new(‘0’, ‘0’) # Bind to all interfaces, with any port
# > 1024.

doh’, I didn’t know about that. sorry for the confusion.

···

At Tue, 10 Sep 2002 00:31:36 +0900, M Mohr wrote:

(a quick grep through netstat shows that I got port 48944 this time).

The client classes (TCPSocket, etc.) will have a port allocated
dynamically in the same way.

Dan

no problemo, it was eductional!

···

On Mon, 2002-09-09 at 12:32, M Mohr wrote:

Dan Debertin airboss@nodewarrior.org writes:

At Tue, 10 Sep 2002 00:31:36 +0900, M Mohr wrote:

oh and one other thing: how to find a free port?

you have to search a free port with trial-and-error AFAIK.
(maybe this can be done more elegant, I’m pretty new to ruby) :

Ouch, ouch, ouch. There’s no need to do this. Just do what you’d do in
C – bind to port 0, and let the system choose a port for you:

s = TCPServer.new(‘0’, ‘0’) # Bind to all interfaces, with any port
# > 1024.

doh’, I didn’t know about that. sorry for the confusion.

(a quick grep through netstat shows that I got port 48944 this time).

The client classes (TCPSocket, etc.) will have a port allocated
dynamically in the same way.

Dan


tom sawyer, aka transami
transami@transami.net