Created
February 15, 2016 13:20
-
-
Save danielmitterdorfer/4897b330a2cf21ebcf04 to your computer and use it in GitHub Desktop.
DNS Cache Sample
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
grant { | |
permission java.net.SocketPermission "*", "resolve"; | |
}; |
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.UnknownHostException; | |
import java.util.Calendar; | |
import java.util.Optional; | |
public class DnsDemo { | |
public static void main(String[] args) throws Exception { | |
while (true) { | |
System.out.printf("%1$tH:%1$tM:%1$tS%n", Calendar.getInstance()); | |
printAddressInfo(addressOf("www.google.com")); | |
printAddressInfo(addressOf("www.this-does-not-exist.io")); | |
Thread.sleep(5000); | |
} | |
} | |
private static Optional<InetAddress> addressOf(String host) { | |
try { | |
return Optional.of(InetAddress.getByName(host)); | |
} catch (UnknownHostException ex) { | |
return Optional.empty(); | |
} | |
} | |
private static void printAddressInfo(Optional<InetAddress> address) { | |
address.ifPresent((a) -> System.out.printf("%s => %s%n", a.getHostName(), a.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
javac DnsDemo.java | |
java -cp . DnsDemo -Djava.security.manager -Djava.security.policy==src/main/resources/dns.policy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment