1.地址解析–发现有多个ipv4地址
![在这里插入图片描述](https://img-blog.csdnimg.cn/3d8cd96d56054f8bb42f140d425ea9cb.png)
2.功能实现
public Class Test{
public static String getRealIpv4() {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual()
|| !netInterface.isUp()) {
continue;
}
if (!netInterface.getDisplayName().contains("Intel")
&& !netInterface.getDisplayName().contains("Realtek")) {
continue;
}
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ip = addresses.nextElement();
if (ip != null) {
if (ip instanceof Inet4Address) {
return ip.getHostAddress();
}
}
}
break;
}
} catch (SocketException e) {
System.err.println("fail=>>>Error when getting host ip address"
+ e.getMessage());
return new String ("fail=>>>Error when getting host ip address"+e.getMessage());
}
return new String("fail=>>>getting host ip address IS NULL");
}
public static void main(String[] args) {
String ip = Test.getRealIpv4()
System.out.println(ip);
}
}