?– Every computer connected to a network has an IP address. An IP address is a number that uniquely identifies each computer on the Net. An IP address is a 32-bit number.
In Java, InetAddress
is a class that represents an Internet Protocol (IP) address. It is part of the java.net
package and provides methods for working with IP addresses. The InetAddress
class can be used to create instances representing both IPv4 and IPv6 addresses.
Here’s a brief overview of some key aspects of the InetAddress
class:
- Creating Instances:
- You can create an
InetAddress
instance by using static factory methods likegetByName()
orgetLocalHost()
. - Example:
java
InetAddress address = InetAddress.getByName("www.example.com");
- You can create an
- Getting Information:
- The class provides methods to retrieve information about the IP address, such as its host name (
getHostName()
), its IP address in byte array form (getAddress()
), and its textual representation (toString()
). - Example:
java
String hostName = address.getHostName();
byte[] ipAddress = address.getAddress();
String ipAddressString = address.toString();
- The class provides methods to retrieve information about the IP address, such as its host name (
- Utility Methods:
InetAddress
also includes methods likeisReachable()
to check if the address is reachable, andgetCanonicalHostName()
to get the fully qualified domain name.- Example:
java
boolean reachable = address.isReachable(1000); // Check if the address is reachable within 1 second.
String canonicalHostName = address.getCanonicalHostName();
In summary, InetAddress
is a Java class used to represent and work with IP addresses, providing methods for creating instances, retrieving information, and performing utility operations related to network addressing.