Know my IP in LAN

Hi how can I know my IP through which two computers communicate in LAN?
Like in socket, I wanna send my IP to the target machine.

Use gethostbyaddr. Toss it in a string and send it along.

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/Socket.html#method-c-gethostbyaddr

Woops. I think ip_address_list is closer to what you want.

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/Socket.html#method-c-ip_address_list

On Sun, Oct 7, 2012 at 12:20 PM, ajay paswan [email protected]
wrote:

Hi how can I know my IP through which two computers communicate in LAN?
Like in socket, I wanna send my IP to the target machine.


Posted via http://www.ruby-forum.com/.

You can can use the system method, as in…

system “”

But, you can easily do it without forking another shell.

Somebody else wrote this…

require ‘socket’
def local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup,
true # turn off reverse DNS resolution temporarily
UDPSocket.open do |s|
s.connect ‘64.233.187.99’, 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end

p local_ip

It checks the ip it would use to route to a random address without out
sending any packets.
Todd

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs