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:


One Response to “How can I find all the ip address’s associated with the local host?”

  1. Param Says:

    Really a nice post. Helped me alot. This requires java 1.6 version.

Leave a Reply

You must be logged in to post a comment.