Java Code To Listing Network Interface Addresses, Run On Windows 10

Posted on

One of the most useful pieces of data you lot tin halt overstep away from a network interface is the listing of IP addresses that are assigned to it. You tin halt obtain this data from a NetworkInterface instance past times using 1 of 2 methods. The get-go method, getInetAddresses(), returns an Enumeration of InetAddress. The other method, getInterfaceAddresses(), returns a listing of java.net.InterfaceAddress instances. This method is used when you lot demand to a greater extent than data almost an interface address beyond its IP address. For example, you lot mightiness demand additional data almost the subnet mask too broadcast address when the address is an IPv4 address, too a network prefix length inward the instance of an IPv6 address.  https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html

The next instance programme lists all the network interfaces too their addresses on a auto (tested on PC running Windows 10):

package javalistinetaddress;  import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Collections; import java.util.Enumeration;  populace cast JavaListInetAddress {      populace static void main(String[] args) throws SocketException {         Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();         for (NetworkInterface netint : Collections.list(nets))             displayInterfaceInformation(netint);     }          static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {         System.out.printf("Display name: %s\n", netint.getDisplayName());         System.out.printf("Name: %s\n", netint.getName());         Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();         for (InetAddress inetAddress : Collections.list(inetAddresses)) {             System.out.printf("InetAddress: %s\n", inetAddress);         }         System.out.printf("\n");      }      }  

Related:
– Java code to listing Network Interface Addresses, run on Raspberry Pi/Raspbian Jessie