Oct 17

You can use the getNetworkPrefixLength() method in the InterfaceAddress class to get the size of the mask.

InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
    System.out.println(address.getNetworkPrefixLength());
}

written by objects \\ tags: , , ,

Oct 01

Use the getNetworkInterfaces() method of the NetworkInterface class to get all the network interfaces, and then loop thru each interface to get the ip addresses associated with that interface.


Enumeration<NetworkInterface> networkInterfaces = 
   NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements())
{
   NetworkInterface networkInterface = 
       networkInterfaces.nextElement();
   List<InterfaceAddress> addresses = 
       networkInterface.getInterfaceAddresses();
   for (InterfaceAddress address : addresses)
   {
      InetAddress inetAddress = address.getInetAddress();
      System.out.println("Name: "+
         inetAddress.getHostName());
      System.out.println("Address: "+
         inetAddress.getHostAddress());
   }
} 

written by objects \\ tags: