1

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();
}

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

Please help me how to achieve this.

3
  • what is the value you are supplying to host.hostname? Commented Feb 3, 2014 at 7:31
  • host.hostname is the ip address of the device Commented Feb 3, 2014 at 7:33
  • You could try doing a reverse DNS (or PTR) lookup, but that will only work if the DNS service your are using has the PTR records for the address range you're using, and the devices have a name to register. Probably, you won't get far with this. Commented Feb 3, 2014 at 7:34

3 Answers 3

5
+25

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have an Android device where getCanonicalHostName() returns only the IP address, however doing a reverse DNS PTR lookup via dig shows that it does have a hostname on the network. Other Android devices on the same network appear not to have this problem.
0

The variable canonicalHostname contains the result. Use

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

1 Comment

canonical name and host name both are printing the same ip address that i have given as input
0

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.

1 Comment

How to know the Which DNS Server iam connecting ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.