How to Retrieve the local IP of a Computer

5 years ago by in How To, Utilities Tagged:

Java provides a simply way to list all IP address of a given computer. Note that a computer can have more than one network card and each network card can have more than one IP address.

for (final Enumeration nie = NetworkInterface.getNetworkInterfaces(); nie.hasMoreElements();) {
  final NetworkInterface ni = nie.nextElement();
  System.out.println("Network Interface: " + ni);
  for (final Enumeration iae = ni.getInetAddresses(); iae.hasMoreElements();) {
    final InetAddress ia = iae.nextElement();
    final String ipAddress = ia.getHostAddress();
    System.out.println("  " + ipAddress);
  }
}

In the above example we are iterating through each network card and we are getting all IP addresses for each network card.

Albert Attard

Albert Attard is a Java passionate and technical lead at a research group. You can find him on . Over the past years Albert worked on various Java projects including traditional server/client applications, modular applications, large data handling applications and concurrent data manipulation applications to name a few. He has a BSc degree from the University of London (Homepage) and an MSc Information Security with the same university. His MSc thesis (Book) received the 2012 SearchSecurity.co.UK award (Website).

Leave a Comment


Time limit is exhausted. Please reload the CAPTCHA.