Skip to content

Instantly share code, notes, and snippets.

@danielmitterdorfer
Created February 15, 2016 13:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmitterdorfer/4897b330a2cf21ebcf04 to your computer and use it in GitHub Desktop.
Save danielmitterdorfer/4897b330a2cf21ebcf04 to your computer and use it in GitHub Desktop.
DNS Cache Sample
grant {
permission java.net.SocketPermission "*", "resolve";
};
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()));
}
}
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