简体   繁体   中英

Getting IP address of local network using java

I want to obtain the local IPv4 address of a user on their phone running my application when connected to a wifi network. Using the following code:

  WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
                    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
                    hostname = ip;

I am able to get something close to the IPv4 address but when compared to the IPv4 address in the command line, it is not exactly the same. Is there a better way to go about this? I know that formatIpAddress is deprecated but until I find a way to get the IPv4 address I'm not too worried about that for now.

EDIT:

I have discovered that the ip address in the phone's wifi settings is what I am getting when using solutions to get the ip address like the suggested solutions. Is there any way to get the ip address in the ip config client side?

The following code traverse all the interface it has and then print the IPv4, IPv6 and mac address of the interface. For LAN IP address u can use a function isSiteLocal() which returns true if IP address is a local address.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class App{

public static void main(String[] args)throws Exception {
    // getting the list of interfaces in the local machine
    Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
    while( n.hasMoreElements()){ //for each interface
        System.out.println("----------------------------------------------------");
        NetworkInterface e = n.nextElement();
    //name of the interface
        System.out.println("Interface Name: " + e.getName());
    /* A interface may be binded to many IP addresses like IPv4 and IPv6
        hence getting the Enumeration of list of IP addresses  */
        Enumeration<InetAddress> a = e.getInetAddresses();
        while( a.hasMoreElements()){
            InetAddress addr = a.nextElement();
            String add = addr.getHostAddress().toString();
            if( add.length() < 17 )
                System.out.println("IPv4 Address: " + add);
            else
                System.out.println("IPv6 Address: " + add);
        }
        if(e.getHardwareAddress() != null){
                    // getting the mac address of the particular network interface
            byte[] mac = e.getHardwareAddress();
                    // properly formatting the mac address
            StringBuilder macAddress = new StringBuilder();
            for(int i =0; i < mac.length; i++){
                macAddress.append(String.format("%03X%s", mac[i],(i < mac.length -1) ? "-":""));
            }
            System.out.println("Hardware adrress: " + macAddress.toString());
        }
        System.out.println("----------------------------------------------------");
    }
}

}

The output of the code in kali linux 2.0 is:
----------------------------------------------------
Interface Name: wlan0
IPv6 Address: fe80:0:0:0:1992:d9bc:7d8c:d85b%wlan0
IPv4 Address: 192.168.1.137
Hardware adrress: 078-0E4-000-0E7-0B0-046
----------------------------------------------------
----------------------------------------------------
Interface Name: lo
IPv6 Address: 0:0:0:0:0:0:0:1%lo
IPv4 Address: 127.0.0.1
----------------------------------------------------

The code print the local IPv4 address of the device in which ur either Android or java application is running.

 try {
        Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();  // gets All networkInterfaces of your device
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface inet = (NetworkInterface) networkInterfaces.nextElement();
            Enumeration address = inet.getInetAddresses();
            while (address.hasMoreElements()) {
                InetAddress inetAddress = (InetAddress) address.nextElement();
                if (inetAddress.isSiteLocalAddress()) {
                    System.out.println("Your ip: " + inetAddress.getHostAddress());  /// gives ip address of your device
                }
            }
        }
    } catch (Exception e) {
        // Handle Exception
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM