On Mon, 20 Nov 2000, Joseph Shraibman wrote:

> Nathan Meyers wrote:
> > 
> > [EMAIL PROTECTED] wrote:
> > 
> > > dear all
> > >        how can i get the localhost ip in java .
> > 
> > InetAddress.getLocalHost()

You need to ask your host what its IP is by using its name.
I haven't had to do this yet, but I'd start with something like this:

InetAddress me = InetAddress.getLocalHost();  // get the object
InetAddress myName = me.getByName( me.getHostName() ); // lookup my name
String myIp = myName.getIpAddress();  // looup my ip address by my name

I haven't tested this, it's just what my own first attempt at this would
be.  BAsically, don't ask the machine for its own ip address, you'll
get a random selection from whatever shows up in its own interface,
including the lo local interface you're trying to avoid.  Ask the machine
for its real name, then ask it for the IP address of the machine which
owns that name.  THis does happen to be the same host, but the system
library will ask the name resolution libs to look it up and will return
the IP address by which that machine can be reached by others.  That's
what you need to find.

------------------------------------------------------------------------
-- 
Joi Ellis                    Software Engineer
Aravox Technologies          [EMAIL PROTECTED], [EMAIL PROTECTED]

No matter what we think of Linux versus FreeBSD, etc., the one thing I
really like about Linux is that it has Microsoft worried.  Anything
that kicks a monopoly in the pants has got to be good for something.
           - Chris Johnson


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to