How To Resolve Network Host Names From IP Address

后端 未结 3 1162
野性不改
野性不改 2020-12-20 06:43

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

相关标签:
3条回答
  • 2020-12-20 06:47

    The variable canonicalHostname contains the result. Use

    holder.computerName.setText("Canonical : "+canonicalHostname);
    
    0 讨论(0)
  • 2020-12-20 06:52

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题