All About Class InetAddress in Java
InetAddress class represents an Internet address and is used when creating DatagramPacket or Socket objects. InetAddress class does not have a public constructor function but instead supports the three static methods which return one or more instances of InetAddress details.
The InetAddress class encapsulates an Internet Protocol (IP) address. InetAddress objects are used by the various classes that are responsible for specifying the destination addresses of outbound network packets, such as DatagramSocket, MulticastSocket, and Socket.
InetAddress does not provide any public constructors. Instead, We must use the static methods getAllByName(), getByName(), and getLocalHost() to create InetAddress objects.
InetAddress class represents an internet protocol (IP) address. The application should use the methods written below to create a new InetAddress instance.:
- getLocalHost()- InetAddress.getLocalHost() returns the InetAddress of the localhost.
- getByName()-InetAddress.getByName() returns the InetAddress of a host specified by name.
- getAllByName()-InetAddress.getAllByName() represents all of the available addresses for a host specified by name.
The structure of class InetAddress is given as:
public final class java.io.InetAddress extends java.lang.Object implements java.io.Serializable{
//methods
public boolean equals(Object obj);
public byte[] getAddress();
public static InetAddress[] getAllByName(String host) throws UnknownHostException;
public static InetAddress getByName(String host) throws UnknownHostException;//determines the IP address of a host,given the hostname
public static InetAddress getByAddresses(byte[] addr);//returns an InetAddress object given the raw IP Address
public static InetAddress getByAddresses(String host,byte[] addr);//returns an InetAddress object
public static InetAddress getLocalHost() throws UnknownHostException;//returns the local host
public String getHostName();//returns the host name for this IP address
public String getHostAddress();//returns the IP address String in textual presentation.
public int hashcode();
public boolean isMulticastAddress();
public String toString();
}
The details of the class structure are given as follows:
public static InetAddress[] getAllByName(String host);
public static InetAddress[] getAllByName(String host) method finds all of the IP addresses that correspond to the given hostname. The hostname can be a machine name or a string representation of an IP address.
This method returns an array of InetAddress objects that corresponds to the given name.
Parameter
host – A String that contains a hostname.
public static InetAddress getByName(String host);
public static InetAddress getByName(String host) method returns the primary IP address that correspond to the given hostname. The hostname can be a machine name, or a string representation of an IP address.
This method returns an InetAddress that corresponds to the given name.
Parameter
host – A String that contains a host name.
public static InetAddress getLocalHost();
public static InetAddress getLocalHost() method finds the IP address of the local machine.
This method returns an InetAddress that corresponds to the name of the local machine.
public boolean equals(Object obj);
public boolean equals(Object obj) method returns true if obj is an instance of InetAddress that specifies the same IP address as the object this method is associated with.
This method returns true if the objects are equivalent; false if they are not.
Parameter
obj – The object to be compared with this object.
public byte[] getAddress();
public byte[] getAddress() method returns the IP address associated with this object as an array of bytes in network order. That means that the first element of the array contains the highest order byte, and the last element of the array contains the lowest order byte.
This method returns a byte array with elements that correspond to the bytes of the IP address that this object represents.
public String getHostAddress();
public String getHostAddress() method returns a string representation of the IP address associated with this object.
This method returns a String that contains the IP address of this object.
public String getHostName();
public String getHostName() method returns the hostname that corresponds to the IP address associated with this object. in most of the cases. However, there are a few special cases:
- If the address associated with this object is the address of the local machine, the method may return null.
- If the method cannot determine a home name to go with the address associated with this object, the method returns a string representation of the address.
- If the application is not allowed to know the hostname, the method returns a string representation of the address.
This method returns the hostname associated with this object.
public int hashCode();
public int hashCode() method returns a hashcode for this object, based on the IP address associated with this object.
This method returns the hashcode based on the IP address of the object.
public boolean isMulticastAddress();
public boolean isMulticastAddress() method returns a flag that indicates if this object represents an IP multicast address. A multicast address is a Class D address, which means that its four highest-order bits are set to 1110. In other words, multicast addresses are
in the range 224.0.0.1 through 239.255.255.255 inclusive.
This method returns true if this object represents a multicast address; false otherwise.
public String toString();
public String toString() method returns a String that contains both the hostname and IP address of this object.
This method returns the string representation of this InetAddress.
Apart from these InetAddress class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)
How Internet Addressing work in Java
Java looks at the whole internet as a collection of host machines -each host is identified by a number called port number. Internet names are in the form of user-readable String like-[email protected] instead of raw numbers.Java handles this situation nicely.
In java.net package this InetAddress class allows us to specify an address in a high-level fashion “host.subdomain.domain“.This class then converts this textual name into 32 bits binary String form (like 192.170.54.34).
The InetAddress class has no visible constructor. In order to create an InetAddress object, we have to use one of the factory methods. Factory methods are static methods that return an instance of the class they reside in.