繁体   English   中英

使用java获取局域网的IP地址

[英]Getting IP address of local network using java

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

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

我能够获得接近IPv4地址的内容,但是与命令行中的IPv4地址相比,它并不完全相同。 有没有更好的方法来解决这个问题? 我知道不赞成使用formatIpAddress,但是直到找到一种获取IPv4地址的方法之前,我现在都不对此太担心。

编辑:

我发现使用解决方案获取建议的解决方案的IP地址时,手机的wifi设置中的IP地址就是我得到的。 有什么方法可以在ip config客户端获取ip地址吗?

以下代码遍历它具有的所有接口,然后打印该接口的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%lo
IPv4地址:127.0.0.1
-------------------------------------------------- -

该代码显示运行AndroidJava应用程序的设备的本地IPv4地址

 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
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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