使用java获取本地网络的IP地址
我想要在连接到wifi网络时获取运行我的应用程序的手机上用户的本地IPv4地址。使用下面的代码:使用java获取本地网络的IP地址
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
hostname = ip;
我能够得到的东西接近IPv4地址,但相比于在命令行的IPv4地址的时候,它是不完全一样的。有没有更好的方法来解决这个问题?我知道formatIpAddress已被弃用,但直到我找到获取IPv4地址的方式,我现在并不太担心这一点。
编辑:
我发现,在手机的WiFi设置了IP地址使用的解决方案时,得到这样的建议的解决方案的IP地址是什么我得到。有什么办法可以在ip config客户端获取IP地址吗?
下面的代码遍历所有它的接口,然后打印的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("----------------------------------------------------");
}
}
}
在卡利的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
-------- --------------------------------------------
的码打印在乌尔任的Android或的java应用程序正在运行该装置的本地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地址的东西,但它不是确切的。*这甚至意味着什么? – shmosel
检查这个问题:https://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device-from-code –
[此链接](https://stackoverflow.com/ questions/40912417/java-getting-ipv4-address)可以帮助你获得IPv4地址。希望能帮助到你。 – Greenkiller