Skip to content

Instantly share code, notes, and snippets.

@michael-pratt
Created July 3, 2018 16:30
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 michael-pratt/a27b10c81b35ada3a4619eb48cad9ae5 to your computer and use it in GitHub Desktop.
Save michael-pratt/a27b10c81b35ada3a4619eb48cad9ae5 to your computer and use it in GitHub Desktop.
Java slow DNS resolution test
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
/**
* Test app to show DNS resolution times for IP -> hostname.
*/
public class InetTester
{
public static void main(String[] args)
{
try
{
List<InetAddress> locAddrs = new ArrayList<>();
for (NetworkInterface itf : asIterable(NetworkInterface.getNetworkInterfaces()))
{
for (InetAddress addr : asIterable(itf.getInetAddresses()))
{
if (!addr.isLinkLocalAddress())
{
locAddrs.add(addr);
}
}
}
// Resolve host names
for (InetAddress addr : locAddrs)
{
System.out.println(new Date().toString() + " Resolving hostname for " + addr);
String hostName = addr.getHostName();
System.out.println(new Date().toString() + " ---> " + hostName);
}
}
catch (SocketException e)
{
e.printStackTrace();
}
}
public static <T> Iterable<T> asIterable(final Enumeration<T> e) {
return new Iterable<T>() {
@Override public Iterator<T> iterator() {
return new Iterator<T>() {
@Override public boolean hasNext() {
return e.hasMoreElements();
}
@SuppressWarnings({"IteratorNextCanNotThrowNoSuchElementException"})
@Override public T next() {
return e.nextElement();
}
@Override public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment