使用java获取本地网络的IP地址

时间:2017-08-17 18:04:40

标签: java android networking

我希望在连接到wifi网络时,在运行我的应用程序的手机上获取用户的本地IPv4地址。使用以下代码:

    public void write(PrintStream output) {
        write(overallRoot, "", output);
    }

    private void write(TreeNode root, String code, PrintStream output) {
        if(root != null) {
            if(root.left == null && root.right == null) {
                output.println(root.data + "\n");
            }
            // recursive-case 
            write(root.left, output);
            write(root.right, output);
        }
    }

我能够获得接近IPv4地址的东西,但是当与命令行中的IPv4地址进行比较时,它并不完全相同。有没有更好的方法来解决这个问题?我知道formatIpAddress已被弃用,但在我找到获取IPv4地址的方法之前,我现在不太担心。

编辑:

我发现手机的wifi设置中的IP地址是我在使用解决方案获取IP地址时所获得的建议解决方案。有没有办法在ip config客户端获取IP地址?

2 个答案:

答案 0 :(得分:0)

以下代码遍历它拥有的所有接口,然后打印接口的IPv4,IPv6和mac地址。对于LAN IP地址,您可以使用一个功能 isSiteLocal(),如果IP地址是本地地址,则返回true。

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("----------------------------------------------------");
    }
}

}

kali linux 2.0中代码的输出是:
-------------------------------------------------- -
接口名称:wlan0
IPv6地址:fe80:0:0:0:1992:d9bc:7d8c:d85b%wlan0
IPv4地址:192.168.1.137
硬件地址:078-0E4-000-0E7-0B0-046
-------------------------------------------------- -
-------------------------------------------------- -
接口名称:lo
IPv6地址:0:0:0:0:0:0:0:1%低位 IPv4地址:127.0.0.1
-------------------------------------------------- -

答案 1 :(得分:0)

代码会打印运行 Android java 应用程序的设备的本地IPv4地址。 / p>

 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
    }