?
Solved

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

Posted on 2002-06-24
4
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 300 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

800 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