In this phrase, we get a hostname when you know the IP address of a remote host, and we get an IP address when you start with the hostname of the remote host. To accomplish both of these tasks, we rely on the InetAddress class. We use the getByName() static method of the InetAddress class to create an InetAddress instance. We can pass either an IP address or a hostname into the getByName() method to create the InetAddress instance. Once we have the InetAddress instance, we can call the getHostAddress() method to return the IP address as a String. If we already know the IP address, we can call the getHostName() method to return the hostname as a String. If the host name cannot be resolved, the getHostName() method will return the IP address. |