I have the following code which is supposed to get only the IPv4 addresses of all active interfaces, but it still returns an IPv6 address on some computers.
public static List<List> getIpAddress() {
List<String> ip = new ArrayList<>();
List<List> ipRefined = new ArrayList<>();
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
ip.add(addresses.nextElement().getHostAddress());
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
for(int x = 0; x < ip.size(); x++){
if(ip.get(x).contains("%")){
try {
if (ip.get(x + 1).contains(".")) {
List<String> tempList = new ArrayList<>();
tempList.add(ip.get(x).substring(ip.get(x).indexOf("%") + 1));
tempList.add(ip.get(x + 1));
ipRefined.add(tempList);
}
} catch (IndexOutOfBoundsException ae) {
}
}
}
return ipRefined;
}
I've tried to specify using only IPv4 by using System.setProperty("java.net.preferIPv4Stack" , "true");
, but this only causes getIpAddress()
to return an empty list. How should I be getting the IPv4 of active interfaces without the use of string manipulation?
EDIT:
Using System.setProperty("java.net.preferIPv4Stack" , "true");
always causes getIpAddress()
to return an empty list.
First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.
If you haven't been keeping up with the official status of IPv4, for all practical purposes those addresses are gone. You can no longer get allotments from the Regional Internet Registries like ARIN (North America), RIPE (Europe) and APNIC (Asia).
In 2022, the IPv4 lease price ranges from $0.39 per IPv4 per month (/16 block in January) to $0.79 per IPv4 per month (/20 block in January).
IPv4 stands for Internet Protocol version 4. It is the underlying technology that makes it possible for us to connect our devices to the web. Whenever a device accesses the Internet, it is assigned a unique, numerical IP address such as 99.48. 227.227.
From InterfaceAddress
This class represents a Network Interface address. In short it's an IP address, a subnet mask and a broadcast address when the address is an IPv4 one. An IP address and a network prefix length in the case of IPv6 address.
Here's my code:
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
System.out.println(String.format("networkInterface: %s", networkInterface.toString()));
if (!networkInterface.isUp()) {
continue;
}
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
int npf = interfaceAddress.getNetworkPrefixLength();
InetAddress address = interfaceAddress.getAddress();
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast == null && npf != 8) {
System.out.println(String.format("IPv6: %s; Network Prefix Length: %s", address, npf));
} else {
System.out.println(String.format("IPv4: %s; Subnet Mask: %s; Broadcast: %s", address, npf, broadcast));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With