In Java, is there any way of getting all the IP addresses associated with a multi-homed system?

Davanum Srinivas

Support has been added in JDK1.4.

  import java.net.InetAddress;
  import java.net.NetworkInterface;
  import java.net.SocketException;
  import java.util.Enumeration;
  
  public class Addrs {
    public static void main(String[] args) throws SocketException {
      Enumeration ifaces = NetworkInterface.getNetworkInterfaces();
  
      while (ifaces.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface)ifaces.nextElement();
        System.out.println(ni.getName() + ":");
  
        Enumeration addrs = ni.getInetAddresses();
  
        while (addrs.hasMoreElements()) {
          InetAddress ia = (InetAddress)addrs.nextElement();
          System.out.println(" " + ia.getHostAddress());
        }
      }
    }
  }
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact