How To Resolve Network Host Names From IP Address

家住魔仙堡 提交于 2019-11-29 12:26:53

I think you might do that this way:

try {
  Log.d("ReverseDNS", "Reverse DNS for 8.8.8.8 is: " + InetAddress.getByName("8.8.8.8").getHostName());
} catch (UnknownHostException e) {
  Log.e("ReverseDNS", "Oh no, 8.8.8.8 has no reverse DNS record!");
}

A few additional things:

  • Take in consideration that this is an operation that might take a long time (understanding as a long time several seconds), so this is absolutely adviced to be done within a Thread or an AsyncTask.

  • Besides the response time, this is a Network Operation, so you'll need to do it outside the UI Thread.

  • Keep also in mind that every host has an associated IP address, but not every IP address has a reverse host, so that operation might fail and you need to handle that too.

  • The DNS server you'll query against is the one of your provider (or the client's provider if you're planning to run this within different clients). That means that not every result will be the same. For instance, your DNS server might not resolve the reverse host of an IP and a different DNS server might do.

The variable canonicalHostname contains the result. Use

holder.computerName.setText("Canonical : "+canonicalHostname);

This is because resolving a host name from an IP Address involves what is called a "Reverse DNS Lookup". The results of such a lookup will vary depending on the DNS server you are connecting to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!