0

Using InetAddress, I'm always getting loopback address even if I am connected to network and the Ip obtained from dhcp is something like : 172.17.13.41. Why ?

I've something like this :

InetAddress address = InetAddress.getLocalHost();
String myIp = address.getHostAddress();
System.out.println(myIp); // I expect the output 172.17.13.41 not 127.0.1.1
harpun
  • 4,022
  • 1
  • 36
  • 40
Winn
  • 413
  • 2
  • 6
  • 17

2 Answers2

0

The same problem is solved in this question: Java getting my IP address

Or how is your problem different?

Community
  • 1
  • 1
  • Ya I got it. But I've few doubts: – Winn Oct 24 '13 at 05:31
  • I'm getting output as 'eth0 fe80:0:0:0:216:d3ff:feeb:eaf9%2' followed by 'eth0 172.17.13.41' what is this first line in output? Is it a Inet-6 address? If so can we filter out Inet-4 and Inet-6 addresses ? – Winn Oct 24 '13 at 05:34
  • And what is the use of iface.isUp() method ? – Winn Oct 24 '13 at 05:44
  • Yes, fe80::/10 should be local addresses in IPv6. I suggest that you try checking the addresses by some regular expression (to distinguish between IPv4 and IPv6). – Simon Stastny Oct 24 '13 at 05:46
-1

Try using:

InetAddress address = InetAddress.getByName("172.17.13.41");
String myIp = address.getHostAddress();
System.out.println(myIp); // I expect the output 172.17.13.41

As per java doc, InetAddress.getLocalHost() may return loop back address.

nullptr
  • 3,320
  • 7
  • 35
  • 68
  • But Thats what I want to find. If I don't know that my IP is 172.17.13.41, how could I use getByName("172.17.13.41") method ? – Winn Oct 24 '13 at 05:38
  • You're kidding, right? If you know the answer you can get the answer? -1 – user207421 Oct 24 '13 at 06:04