如何从IP地址解析网络主机名 [英] How To Resolve Network Host Names From IP Address

查看:1387
本文介绍了如何从IP地址解析网络主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究基于wifi的聊天引擎,我可以通过以下方式检索连接到当前wifi网络的主机列表这个链接现在得到了带有ip地址的设备列表,但是我需要来自ip地址的主机名并尝试跟随

I am working on wifi based chat engine and I was able to retrieve the list of hosts connected to current wifi network by followin this link and now got list of devices with ip addresses but i need host name from the ip address and tried following

InetAddress inetAddr;
try {
    inetAddr = InetAddress.getByName(host.hostname);
    String hostname = inetAddr.getHostName();
    String canonicalHostname = inetAddr.getCanonicalHostName();
    holder.computerName.setText("Canonical : "+host.hostname);
} catch (Exception e) {
    e.printStackTrace();
}

这里主机名和规范主机名都显示ip地址而不是主机姓名。

Here the host name and canonical host name both are displaying ip address rather than host name.

请帮我解决这个问题。

推荐答案

我想你可以这样做:

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!");
}

还有一些额外的东西:


  • 考虑到这是一项可能需要很长时间的操作(长时间理解几秒钟),所以绝对建议在<$内完成c $ c>线程或 AsyncTask

除了响应时间,这是一个网络操作,所以你需要在UI线程之外做。

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

请记住,每个主机都有一个关联的IP地址,但并非每个IP地址都有一个反向主机,因此操作可能会失败并且您需要来处理它。

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.

您要查询的DNS服务器是您的提供商(或者您的提供商) '计划在不同的客户端运行它)。这意味着并非每个结果都是一样的。例如,您的DNS服务器可能无法解析IP的反向主机,而另一个DNS服务器可能无法解析。

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.

这篇关于如何从IP地址解析网络主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆