Solved

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

Posted on 2002-06-24
4
205 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 81
map related example 6 47
MySqlDump not dumping triggers 1 29
hibernate example using maven 12 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now