I have to admit, this is one clever little trick to allow a program to get the local IP address of an interface that is network reachable (i.e. not localhost).
In one line in Ruby:
UDPSocket.open {|s| s.connect(’64.233.187.99′, 1); s.addr.last }
Of course, it can be done in other languages too. The trick is to setup a socket connection to something real that requires no handshake (UDP) and then look at the socket address given back by the OS. You could do this with TCP too, but then you'd be sending packets and trying to setup a connection and all manner of nonsense could occur.
Never heard of that one, but will pass it along to some folks... :-) Thanks!
ReplyDeleteYeah, very new to me.
Delete