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



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

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

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

编辑:

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

代码打印运行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
}

以下代码遍历它拥有的所有接口,然后打印接口的 IPv4、IPv6 和 mac 地址。对于局域网 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

----------------------------------------------------

最新更新