Getting the local IP address! (was Re: Scanning machines on a network)

Dar Scott dsc at swcp.com
Fri Mar 15 02:02:00 CST 2002


On Thursday, March 14, 2002, at 11:03 PM, andu wrote:
> On Linux as expected it doesn't work.

Actually, I naively thought it would.  Does it work for an Internet 
address (such as that for yahoo.com) or for an arbitrary address in 
your LAN subnet?

> What exactly are you opening the
> socket to

Broadcast.  I could have chosen some other address, such as the one 
for yahoo.com that Sarah's script used.  (I have a vague hope that 
on a multihomed system 255.255.255.255 would get the primary IP 
address, but I have no real reason to think this.  Also, I prefer a 
method that would work even on a LAN without Internet access.  
Using a valid Internet IP address on a multihomed system will 
probably get the address for an adaptor with Internet access.)

> and what good would that ip address be if you're not networked
> as you imply

Whoops.  I didn't mean to imply that.  Sarah's method depends on 
being connected to the Internet.  I didn't want to be limited to 
that.  Another advantage of not making a connection is related to 
manners; I was not comfortable with connecting to yahoo.com to find 
out my IP address.  (For me it is also a matter of security; I 
don't want computers on some of my systems babbling more than they 
need to.)

> or if you are networked and get your IP address from DHCP
> which happens more often then not.

Using DHCP should not keep this from working.  However, an app 
should not store this to disk for use next time it is run; it 
should find it each time it is run.

Why would an application need to know its own computer's IP 
address?  Well...

Well, here is one reason, the one that prompted this discussion:  I 
had suggested (in a response to a query by Richard) that one way to 
check on serial number clashes in applications is to have all of 
those apps broadcast serial numbers on the LAN.  (To some port he 
might pick out of a hat.)  All apps would receive the broadcasts.  
A serial number clash occurs if an app receives one that is the 
same as its AND it comes in a datagram from _another_ computer.  
One way to check that it came from another computer is to check the 
IP address.

Also, I know one company that uses the IP address to check that an 
app (part of a complex distributed system) is running on the right 
computer.

> For same machine communication
> 127.0.0.1 is the standard.

Using that for this method will get you 127.0.0.1 in all cases that 
I've seen.

> Luckily you can not cut yourself with it.

I find 127.0.0.1 to be useful.

Dar Scott




More information about the use-livecode mailing list