Skip to content

Instantly share code, notes, and snippets.

@m-manu
Created August 23, 2018 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-manu/d39f5cf919ee182e4307473ae1859e7c to your computer and use it in GitHub Desktop.
Save m-manu/d39f5cf919ee182e4307473ae1859e7c to your computer and use it in GitHub Desktop.
Some network utils
class NetworkUtils {
public static String getLocalIp() {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback() || !networkInterface.isUp() || networkInterface.isVirtual() || networkInterface.isPointToPoint()) {
continue;
}
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
final String ip = address.getHostAddress();
if (Inet4Address.class == address.getClass()) {
return ip;
}
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment