Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get IP_ADDRESS in IPV4 format

Tags:

I am trying to get the IP address of an device i.e using WIFI or 3G connection. I am getting the ip address in IPV6 format which is not understandable. I want in IPV4 format IP address.I have done google but dint found any proper solutions.

here is code which I am using to get IP address of an device

public String getLocalIpAddress() {     try {         try {         for (Enumeration<NetworkInterface> en = NetworkInterface                 .getNetworkInterfaces(); en.hasMoreElements();) {             NetworkInterface intf = en.nextElement();             for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)              {                 InetAddress inetAddress = enumIpAddr.nextElement();                 System.out.println("ip1--:" + inetAddress);                 System.out.println("ip2--:" + inetAddress.getHostAddress());                 if (!inetAddress.isLoopbackAddress()) {                       String ip = inetAddress.getHostAddress().toString();                     System.out.println("ip---::" + ip);                     EditText tv = (EditText) findViewById(R.id.ipadd);                     tv.setText(ip);                     return inetAddress.getHostAddress().toString();                  }             }         }     } catch (Exception ex) {         Log.e("IP Address", ex.toString());     }     return null; } 

I am getting this ouput :

ip1--:/fe80::5054:ff:fe12:3456%eth0%2 ip2--:fe80::5054:ff:fe12:3456%eth0 

It should be displayed like this :

192.168.1.1 

please help me out..

like image 889
Rahul Baradia Avatar asked Jun 13 '12 13:06

Rahul Baradia


People also ask

What is IPv4 format of a IP address?

An IPv4 address has the following format: x . x . x . x where x is called an octet and must be a decimal value between 0 and 255.

How do I get an IPv4 address?

On an Android/tablet Go to your Wifi network settings, then select the network you're connected to. You'll find your IP address along with the other network information.

What is an example of an IPv4 address format?

IP (version 4) addresses are 32-bit integers that can be expressed in hexadecimal notation. The more common format, known as dotted quad or dotted decimal, is x.x.x.x, where each x can be any value between 0 and 255. For example, 192.0. 2.146 is a valid IPv4 address.

How do I get IPv4 from IPv6?

While there are IPv6 equivalents for the IPv4 address range, you can't convert all IPv6 addresses to IPv4 - there are more IPv6 addresses than there are IPv4 addresses. The only sane way around this issue is to update your application to be able to understand and store IPv6 addresses.


2 Answers

After trying many tricks.. finally I can get the IP address in IPV4 format.. Here is my code..

public String getLocalIpAddress() {     try {         for (Enumeration<NetworkInterface> en = NetworkInterface                 .getNetworkInterfaces(); en.hasMoreElements();) {             NetworkInterface intf = en.nextElement();             for (Enumeration<InetAddress> enumIpAddr = intf                     .getInetAddresses(); enumIpAddr.hasMoreElements();) {                 InetAddress inetAddress = enumIpAddr.nextElement();                 System.out.println("ip1--:" + inetAddress);                 System.out.println("ip2--:" + inetAddress.getHostAddress());        // for getting IPV4 format       if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) {                      String ip = inetAddress.getHostAddress().toString();                     System.out.println("ip---::" + ip);                     EditText tv = (EditText) findViewById(R.id.ipadd);                     tv.setText(ip);                     // return inetAddress.getHostAddress().toString();                     return ip;                 }             }         }     } catch (Exception ex) {         Log.e("IP Address", ex.toString());     }     return null; } 

Added if condition as shown below

 /**This shows IPV4 format IP address*/  if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())){} 

instead of this

 /**This shows IPV6 format IP address*/  if (!inetAddress.isLoopbackAddress()){} 

Many Thanks.. Rahul

An alternative for checking if the address is a version 4 address is:

if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) 
like image 168
Rahul Baradia Avatar answered Oct 18 '22 15:10

Rahul Baradia


You cannot assume that any device has just one network address. You also cannot assume that it will have any IPv4 - it may be IPv6 only, so your application will need to be able to handle both IPv4 and IPv6 address displays.

Typically, an Android phone has at least two interfaces that get assigned usable ip addresses, rmnet0 for the 3G data, which for IPv4 is often carrier-grade NATed and so cannot accept incoming socket connections, and may also have an IPv6 address; and wlan0 for the wifi, which will have whatever IPv4 and/or IPv6 address it can negotiate with the network it attaches to.

Some versions of Android will intentionally drop the (typically more expensive) rmnet0 link when it attaches to wifi - in an attempt to reduce 3G data usage. This behaviour is a problem when the wifi has attached to something that is a captive portal that requires a manual sign-in.

like image 27
Simon Hewison Avatar answered Oct 18 '22 15:10

Simon Hewison