1

Hi guys I've gone through a lot of coding on line to get my android mobiles IP address Most of them are ending with

if (!inetAddress.isLoopbackAddress())
                { return inetAddress.getHostAddress().toString();     } 

How ever i get something that looks like this:- "fe80::a00:27ff:fe37:28b5%eth1" Weird cause I was expecting something like xxx.xxx.xxx.xxx

Can some one help me understand whats this?

Wadia
  • 36
  • 1
  • 4

3 Answers3

2

That is an IPv6 address. Additionally, since it starts with fe80:: you know it's also a link-local IPv6 address, so cannot be used for communication beyond the local network. (in this case, eth1, since that is the scope specified at the end after the % - but note that using a % to identify the scope isn't always valid when using an IPv6 address.)

Community
  • 1
  • 1
mpontillo
  • 13,559
  • 7
  • 62
  • 90
  • Great! Now here's what I did- I got this Ipv6 address of my phone (server) and placed it in my Emulator (client), but it failed to connect. Then I did it the old fashion way xxx.xxx.xxx.xxx format and it connected! So any way I could directly get the old format as this Ipv6 isnt working any ways? – Wadia Dec 02 '14 at 09:07
  • @Wadia, there are a number of possible approaches to filter out the unwanted IPv6 addresses (see the other answers to this question). One approach I saw was to check if a `:` exists in the address. If so, it's an IPv6 address. – mpontillo Dec 02 '14 at 14:25
1

Try java.net.Inet4Address instead.

MeetTitan
  • 3,383
  • 1
  • 13
  • 26
1

Its returning IPV6 address.

Check if its IPV4 address before returning result.

if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
    return inetAddress.getHostAddress().toString(); 
}

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124