Skip to content

Instantly share code, notes, and snippets.

@ShmuelMofrad
Created November 29, 2017 18:55
Show Gist options
  • Save ShmuelMofrad/7db49ea35e0422d7c66b9af7797da28b to your computer and use it in GitHub Desktop.
Save ShmuelMofrad/7db49ea35e0422d7c66b9af7797da28b to your computer and use it in GitHub Desktop.
IPv4 - represents a Network Interface address
import java.net.Inet4Address;
import java.net.UnknownHostException;
public class InternetProtocol {
public static void main(String[] args) throws UnknownHostException {
representsInet4Address();
}
private static void representsInet4Address() throws UnknownHostException {
System.out.println(
Inet4Address.getByName(
Inet4Address.getLocalHost()
.getCanonicalHostName())
.getHostAddress());
}
}
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class IPv4 {
public static void main(String[] args) throws SocketException {
getIPv4();
}
private static void getIPv4() throws SocketException {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback() && ni.isUp()) {
System.out.println(ni);
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia.getAddress().getAddress().length == 4) {
System.out.println(ia.getAddress());
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment