InetAddress
Get hostname from IP address
This is an example of how to get the host name of a remote host from its IP address. Doing hostname resolution from IP addresses implies that you should :
- Retrieve the Address object for the specified host that contains all address related information about the specific host
- Use the
getHostName()
and/orgetCanonicalHostName()
API methods to retrieve the hostname and/or canonical hostname of the specific host
as described in the code snippet below.
If the specific host address exists and there is no connectivity issues between the client and the host machines then you should be able to get the hostname from the designated host IP address.
package com.javacodegeeks.snippets.core; import java.net.InetAddress; import java.net.UnknownHostException; public class GetHostnameFromIPAddress { public static void main(String[] args) { try { InetAddress inetAddr = InetAddress.getByName("216.239.34.21"); // Get the host name String hostname = inetAddr.getHostName(); // Get canonical host name String canonicalHostname = inetAddr.getCanonicalHostName(); System.out.println("Hostname: " + hostname); System.out.println("Canonical Hostname: " + canonicalHostname); } catch (UnknownHostException e) { System.out.println("Host not found: " + e.getMessage()); } } }
This was an example of how to get the hostname from the IP address of a specific host in Java.
i want to ask i have ip address but after i tried to convert to device name cannot retruning device name only returning ip address can you help me for doing it thanks