Sometimes one wishes to get all the interfaces on a machine. The code given below does exactly that.
public class IPAdress {
public void getInterfaces (){
try {
Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface netface = (NetworkInterface)
e.nextElement();
System.out.println("Net interface: "+netface.getName());
Enumeration e2 = netface.getInetAddresses();
while (e2.hasMoreElements()){
InetAddress ip = (InetAddress) e2.nextElement();
System.out.println("IP address: "+ip.toString());
}
}
}
catch (Exception e) {
System.out.println ("e: " + e);
}
}
public static void main(String[] args) {
IPAdress ip = new IPAdress();
ip.getInterfaces();
}
}