Java NetworkInterface get IP addresses

Description

Java NetworkInterface get IP addresses

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class Main {
   public static void main(String args[]) throws Exception {
      System.setProperty("java.net.preferIPv4Stack", "true");
      Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces();
      while (intfs.hasMoreElements()) {
         NetworkInterface intf = intfs.nextElement();
         Enumeration<InetAddress> addresses = intf.getInetAddresses();
         if (addresses.hasMoreElements()) {
            System.out.println("\nName: " + intf.getName());
            System.out.println("Display name: " + intf.getDisplayName());
            while (addresses.hasMoreElements()) {
               InetAddress addr = addresses.nextElement();
               System.out.println("Address: " + addr);
            }//from   w  w  w. ja  v  a 2  s . c o  m
         }
      }
   }
}



PreviousNext

Related