Skip to content

Instantly share code, notes, and snippets.

@martinpaoloni
Last active December 17, 2015 17:09
Show Gist options
  • Save martinpaoloni/5644297 to your computer and use it in GitHub Desktop.
Save martinpaoloni/5644297 to your computer and use it in GitHub Desktop.
Get IP Addresses in Java
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class Test {
/**
* @param args
* @throws SocketException
*/
public static void main(final String[] args) throws SocketException {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
System.out.println(ni);
Enumeration<InetAddress> ias = ni.getInetAddresses();
while (ias.hasMoreElements()) {
InetAddress ia = ias.nextElement();
System.out.println(" " + ia.getHostAddress());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment