Java reporting wrong IP address in SuSE 9.2

Discussion in 'Java' started by subscriber@liljenberg.net, Mar 18, 2005.

  1. Guest

    I have a strange problem. When I try to determine my machine's IP
    address from Java (ver 1.4.2_06), I get the wrong result. It reports
    192.168.0.88 when the address is actually 192.168.0.60.
    I am running SuSE 9.2.

    Here's the code (which gives the output "algw-05/192.168.0.88"):

    public class TestMain {
    public static void main(String[] args) {
    try {
    String myHostName =
    InetAddress.getLocalHost().getHostName();
    InetAddress[] myAddresses =
    InetAddress.getAllByName(myHostName);
    for (int i = 0; i < myAddresses.length; i++) {
    System.out.println(myAddresses.toString());
    }
    }
    catch (UnknownHostException ignored) {
    }
    }
    }

    And here's the output from ifconfig:

    ath0 Link encap:Ethernet HWaddr 00:05:4E:46:6F:7D
    inet addr:192.168.0.60 Bcast:192.168.0.255
    Mask:255.255.255.0
    inet6 addr: fe80::205:4eff:fe46:6f7d/64 Scope:Link
    UP BROADCAST NOTRAILERS RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:19954 errors:148 dropped:0 overruns:0 frame:148
    TX packets:17833 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:199
    RX bytes:18750042 (17.8 Mb) TX bytes:2639289 (2.5 Mb)
    Interrupt:11 Memory:f9220000-f9230000

    Any ideas?
    // Ake
     
    , Mar 18, 2005
    #1
    1. Advertising

  2. On 18 Mar 2005 13:08:21 -0800, wrote:
    > I have a strange problem. When I try to determine my machine's IP
    > address from Java (ver 1.4.2_06), I get the wrong result. It reports
    > 192.168.0.88 when the address is actually 192.168.0.60.
    > I am running SuSE 9.2.


    It sounds to me like you need to check the contents of your /etc/hosts
    or /etc/nsswitch.conf.

    What hostname does this line report:
    > InetAddress.getLocalHost().getHostName();


    And what does nsloookup <yourhostname> report?

    What happens if you use
    java.net.NetworkInterface.getNetworkInterfaces() to iterate over the
    available interfaces, and then call getInetAddresses() in each?

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Mar 18, 2005
    #2
    1. Advertising

  3. Thanks for your help Gordon.
    For some reason the /etc/hosts file contained the wrong IP address.

    The code below (as you suggested) gave the correct IP of course:

    Enumeration en = java.net.NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
    NetworkInterface ni = (NetworkInterface) en.nextElement();
    System.out.println(ni);
    }

    // Ake
     
    =?iso-8859-1?B?xWtlIEw=?=, Mar 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Willy Kreim
    Replies:
    2
    Views:
    665
  2. Deshaies
    Replies:
    1
    Views:
    1,319
  3. Replies:
    1
    Views:
    538
  4. Naresh Agarwal
    Replies:
    5
    Views:
    5,088
  5. Replies:
    2
    Views:
    3,213
    Jon Martin Solaas
    May 13, 2006
Loading...

Share This Page