Last active
December 17, 2015 17:09
-
-
Save martinpaoloni/5644297 to your computer and use it in GitHub Desktop.
Get IP Addresses in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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