2

Possible Duplicate:
java InetAddress.getLocalHost(); returns 127.0.0.1 … how to get REAL IP?

I have run the below code in different operation system. When i run this in ubuntu 11.4 I got 127.0.1.1 but the same time i got actual ip 192.XXX.XXX.XXX in windows. How can i get actual ip?

import java.net.*;
import java.io.*;

public class GetIPAddress {
   public static void main(String [] args) {
   try {
        InetAddress thisIp =InetAddress.getLocalHost();
        System.out.println("IP:"+thisIp.getHostAddress());
       }
      catch(Exception e) {
              e.printStackTrace();
       }
    }
}

Cheers..

Community
  • 1
  • 1
Dhinakar
  • 4,061
  • 6
  • 36
  • 68
  • 1
    You'll have to query an IP on a specific device. It just so happens that the `localhost` address resolves to `127..` on one OS and `192..` on other. What was the OS that gave you `192..`? – nullpotent Oct 05 '12 at 11:56
  • 1
    One host can have many different IP addresses at the same time, there is no single one that is the "actual IP", they're all valid for different purposes. – Ian Roberts Oct 05 '12 at 11:59
  • @iccthedral I got actual ip in windows environment. – Dhinakar Oct 05 '12 at 12:01

0 Answers0