What about doing something like this:

InetAddress me = InetAddress.getLocalHost();  // get the object
InetAddress myName = me.getByName( me.getHostName() ); // lookup my name
InetAddress IPs = InetAddress.getAllByName(myName);

Peace.......

Tom




Juergen Kreileder <[EMAIL PROTECTED]> on 11/21/2000 07:10:37 PM

To:   Joi Ellis <[EMAIL PROTECTED]>
cc:   Joseph Shraibman <[EMAIL PROTECTED]>, Nathan Meyers
      <[EMAIL PROTECTED]>, [EMAIL PROTECTED],
      [EMAIL PROTECTED]
Subject:  Re: localhost ip


>>>>> "Joi" == Joi Ellis <[EMAIL PROTECTED]> writes:

    Joi> 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()

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

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

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

It's not that easy.  A host may have several IP addresses and other
hosts may have to use different addresses to reach it.  E.g. hosts on
the intranet may have to use 192.168.0.100, but external machines may
have to use 65.123.66.124.
If you have a socket connection to another host you can call
Socket.getLocalAddress() to find out which local address is used for
the socket.


        Juergen

--
Juergen Kreileder, Blackdown Java-Linux Team
http://www.blackdown.org/java-linux.html
JVM'01: http://www.usenix.org/events/jvm01/


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






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

Reply via email to