Solved

How to get the PRACTICAL IP address of my linux box?

Posted on 2002-06-24
4
208 Views
Last Modified: 2010-03-31
I have a Linux box using DHCP to get it's IP address.

I am writing a Java program to show the IP address, I used the following code:

myIP = InetAddress.getLocalHost().getHostAddress();

however, the result of the above code is "127.0.0.1" -- brings me no information....

How does the getHostAddress() work? and in what situation it will return a practical IP address instead of the loopback IP?

Thanks!

Bun
0
Comment
Question by:bunwong
4 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 100 total points
ID: 7106570
try this code

        String hostName = new String();
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch(UnknownHostException uhe) {
            System.err.println("Exception getting host: " +
                uhe.toString());
        }
        System.out.println("Hostname is: " + hostName);

        InetAddress[] allInterfaces=null;
        try {
            allInterfaces = InetAddress.getAllByName(hostName);
        } catch(UnknownHostException uhe) {
            System.err.println("Exception getting host: " +
                uhe.toString());
        }

        for(int x=0;x<allInterfaces.length;x++) {
            System.out.println("IP address " + x+1 + ": " +
                allInterfaces[x].getHostAddress());
        }      



(copy and pasted from
http://groups.google.com/groups?q=Java+%22get+all+IP+addresses%22&hl=en&lr=&ie=UTF-8&oe=utf-8&selm=Hl7E7.4903%24Fr3.100304%40news1.oke.nextra.no&rnum=1
)
0
 

Author Comment

by:bunwong
ID: 7107645
Hi Heyhey,

Thank you for your comment. However, the code you gave me does not work.

It is able to get the name "localhost" and the IP address "127.0.0.1", but it does not get the other IP address "192.168.1.104".

The IP address "192.168.1.104" is assigned by a DHCP server, the address works on all my IP related tools, e.g. ping, ftp, telnet, vnc.

bun
0
 
LVL 35

Expert Comment

by:girionis
ID: 8860678
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this
question is:

- points to heyhey_

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question