Serversocket.GetMyIP returns strange result

Status
Not open for further replies.

corwin42

Expert
Licensed User
Longtime User
In the past I checked for an internet connection with Serversocket.GetMyIP.
When it returns 127.0.0.1 the device was not connected.

On my Galaxy Nexus with JB 4.1.1 Serversocket.GetMyIP returns "fe80::2064:32ff:fe5e:9660%p2p0" even if there is no connection. Any ideas why this happens?

I now check with Serversocket.GetMyWifiIP (if it is 127.0.0.1 wifi is not connected) and with Phone.GetDataState (If it is CONNECTED, then data is connected).

Any better ways to check for a valid data connection?
 

ggpanta

Member
Licensed User
Longtime User
The result you get is a link-local ip for IPv6 (fe80::/64), in IPv4 the link-local ip would be in the 169.254.0.0/16 range.
These ranges are provided from the OS, if you have an automated way to setup IPs (DHCP or autoconf) and no IP has been provided yet.

If a device has an ip in those ranges then you should just use them the same way you used to use localhost (ie no conectivity).

Technically its not the same though since those are properly routable ranges.

Edit:
Just wanted to add that you may also see a ::1 this is the IPv6 equivalent of 127.0.0.1
 
Last edited:
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Thanks for your info. Then I think that GetMyIP is not the best way to check for Internet connection anymore. I currently do it the way I explained above.

Gesendet von meinem Galaxy Nexus mit Tapatalk 2
 
Upvote 0

ggpanta

Member
Licensed User
Longtime User
What I do is do a ping on a well known host to check if I have Internet connectivity, just make sure to use an IP instead of a hostname cause you are not sure if the phone has DNS the moment you initiate the ping.

I use 8.8.8.8 which is a public facing DNS cluster from google.
 
Upvote 0
Status
Not open for further replies.
Top