Solved

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

Posted on 2002-06-24
4
200 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 61
network + 7 73
wordappend challenge 8 83
Java - Why doesn't this JFrame work 3 14
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

12 Experts available now in Live!

Get 1:1 Help Now