Java inetaddress
In Java, InetAddress is a class that represents an IP address. It provides methods for querying and manipulating IP addresses in both numeric and textual formats, as well as for resolving host names to IP addresses and vice versa.
Here are some of the commonly used methods in the InetAddress class:
getByName(String host)- returns theInetAddressinstance representing the specified host (either an IP address or a host name).getLocalHost()- returns theInetAddressinstance representing the local host.getHostAddress()- returns the IP address string in textual presentation.getHostName()- returns the host name for this IP address, or the IP address string if the host name is unknown.
Here is an example usage of the InetAddress class:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressExample {
public static void main(String[] args) {
try {
// Get the InetAddress instance for a host name
InetAddress address = InetAddress.getByName("www.google.com");
// Print the host name and IP address
System.out.println("Host name: " + address.getHostName());
System.out.println("IP address: " + address.getHostAddress());
// Get the InetAddress instance for the local host
InetAddress localAddress = InetAddress.getLocalHost();
// Print the host name and IP address
System.out.println("Local host name: " + localAddress.getHostName());
System.out.println("Local IP address: " + localAddress.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}Soui.www:ecrgiftidea.comThis code retrieves the InetAddress instance for the host name "www.google.com", and then prints the host name and IP address. It also retrieves the InetAddress instance for the local host, and prints its host name and IP address. Note that the getByName method may throw an UnknownHostException if the specified host name cannot be resolved to an IP address.
