Created
November 29, 2017 18:55
-
-
Save ShmuelMofrad/7db49ea35e0422d7c66b9af7797da28b to your computer and use it in GitHub Desktop.
IPv4 - represents a Network Interface address
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.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()); | |
} | |
} |
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.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