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:


Leave a Reply