- Java inet address class in java code example
- Java InetAddress class
- IP Address
- TCP/IP Protocol
- Java InetAddress Class Methods
- Example of Java InetAddress Class
- InetAddress Class in Java
- Class Inet6Address
- Textual representation of IP addresses
- Special IPv6 address
- Textual representation of IPv6 scoped addresses
Java inet address class in java code example
getAllByName() Returns an array of IP addresses for the given host getLoopbackAddress() Returns the loopback address getHostAddress() Returns IP address in textual form. IP provides the address to the nodes connected on the internet.
Java InetAddress class
Java InetAddress class represents an IP address. The java.net.InetAddress class provides methods to get the IP of any host name for example www.javatpoint.com, www.google.com, www.facebook.com, etc.
An IP address is represented by 32-bit or 128-bit unsigned number. An instance of InetAddress represents the IP address with its corresponding host name. There are two types of addresses: Unicast and Multicast. The Unicast is an identifier for a single interface whereas Multicast is an identifier for a set of interfaces.
Moreover, InetAddress has a cache mechanism to store successful and unsuccessful host name resolutions.
IP Address
- An IP address helps to identify a specific resource on the network using a numerical representation.
- Most networks combine IP with TCP (Transmission Control Protocol). It builds a virtual bridge among the destination and the source.
There are two versions of IP address:
1. IPv4
IPv4 is the primary Internet protocol. It is the first version of IP deployed for production in the ARAPNET in 1983. It is a widely used IP version to differentiate devices on network using an addressing scheme. A 32-bit addressing scheme is used to store 2 32 addresses that is more than 4 million addresses.
Features of IPv4:
- It is a connectionless protocol.
- It utilizes less memory and the addresses can be remembered easily with the class based addressing scheme.
- It also offers video conferencing and libraries.
2. IPv6
IPv6 is the latest version of Internet protocol. It aims at fulfilling the need of more internet addresses. It provides solutions for the problems present in IPv4. It provides 128-bit address space that can be used to form a network of 340 undecillion unique IP addresses. IPv6 is also identified with a name IPng (Internet Protocol next generation).
Features of IPv6:
- It has a stateful and stateless both configurations.
- It provides support for quality of service (QoS).
- It has a hierarchical addressing and routing infrastructure.
TCP/IP Protocol
- TCP/IP is a communication protocol model used connect devices over a network via internet.
- TCP/IP helps in the process of addressing, transmitting, routing and receiving the data packets over the internet.
- The two main protocols used in this communication model are:
- TCP i.e. Transmission Control Protocol. TCP provides the way to create a communication channel across the network. It also helps in transmission of packets at sender end as well as receiver end.
- IP i.e. Internet Protocol. IP provides the address to the nodes connected on the internet. It uses a gateway computer to check whether the IP address is correct and the message is forwarded correctly or not.
Java InetAddress Class Methods
Method | Description |
---|---|
public static InetAddress getByName(String host) throws UnknownHostException | It returns the instance of InetAddress containing LocalHost IP and name. |
public static InetAddress getLocalHost() throws UnknownHostException | It returns the instance of InetAdddress containing local host name and address. |
public String getHostName() | It returns the host name of the IP address. |
public String getHostAddress() | It returns the IP address in string format. |
Example of Java InetAddress Class
Let’s see a simple example of InetAddress class to get ip address of www.javatpoint.com website.
InetDemo.java
import java.io.*; import java.net.*; public class InetDemo< public static void main(String[] args)< try< InetAddress ip=InetAddress.getByName("www.javatpoint.com");System.out.println("Host Name: "+ip.getHostName()); System.out.println("IP Address: "+ip.getHostAddress()); >catch(Exception e) > >
Host Name: www.javatpoint.com IP Address: 172.67.196.82
Program to demonstrate methods of InetAddress class
InetDemo2.java
import java.net.Inet4Address; import java.util.Arrays; import java.net.InetAddress; public class InetDemo2 < public static void main(String[] arg) throws Exception < InetAddress ip = Inet4Address.getByName("www.javatpoint.com"); InetAddress ip1[] = InetAddress.getAllByName("www.javatpoint.com"); byte addr[]=; System.out.println("ip : "+ip); System.out.print("\nip1 : "+ip1); InetAddress ip2 = InetAddress.getByAddress(addr); System.out.print("\nip2 : "+ip2); System.out.print("\nAddress : " +Arrays.toString(ip.getAddress())); System.out.print("\nHost Address : " +ip.getHostAddress()); System.out.print("\nisAnyLocalAddress : " +ip.isAnyLocalAddress()); System.out.print("\nisLinkLocalAddress : " +ip.isLinkLocalAddress()); System.out.print("\nisLoopbackAddress : " +ip.isLoopbackAddress()); System.out.print("\nisMCGlobal : " +ip.isMCGlobal()); System.out.print("\nisMCLinkLocal : " +ip.isMCLinkLocal()); System.out.print("\nisMCNodeLocal : " +ip.isMCNodeLocal()); System.out.print("\nisMCOrgLocal : " +ip.isMCOrgLocal()); System.out.print("\nisMCSiteLocal : " +ip.isMCSiteLocal()); System.out.print("\nisMulticastAddress : " +ip.isMulticastAddress()); System.out.print("\nisSiteLocalAddress : " +ip.isSiteLocalAddress()); System.out.print("\nhashCode : " +ip.hashCode()); System.out.print("\n Is ip1 == ip2 : " +ip.equals(ip2)); > >
In the above Java code, the various boolean methods of InetAdress class are demonstrated.
Java.net.InetAddress Class in Java, The java.net.InetAddress class provides methods to get the IP address of any hostname. An IP address is represented by 32-bit or 128-bit unsigned number. InetAddress can handle both IPv4 and IPv6 addresses. There are 2 types of addresses : Unicast — An identifier for a single interface. Multicast — …
InetAddress Class in Java
An IP address is an address having information about how to reach a specific host which is a 32-bit unique address number having an address space of 2^32. InetAddress class is a representation of an IP address. It represents both the 32-bit IPv4 address and the 128-bit IPv6 address. It is the superclass of Inet6Address and Inet4Address classes. An instance of this class consists of an IP address and usually a hostname depending on whether hostname resolution was performed during the creation.
Note: There are no constructors for this class but static methods which return instances of InetAddress class for general use
Methods of InetAddress Class
Method | Action Performed |
---|---|
equals() | Returns true if this IP address is the same as that of the object specified. Equals() method don’t consider hostnames while comparing and only consider IP address associated. |
getAddress() | Returns the raw IP address of this InetAddress object as an array. The order in which bytes appear in an array is the same as in IP address i.e. getAddress[0] will contain the highest order byte. |
getByAddress() | Create an InetAddress object. It takes the hostname and IP address as its parameter. The hostname can be the machine name as in “www.geeksforgeeks.org” or its textual IP address. |
getByName() | Returns the IP Address of the host specified. If the host is a literal IP address, then only its validity is checked. |
getAllByName() | Returns an array of IP addresses for the given host |
getLoopbackAddress() | Returns the loopback address |
getHostAddress() | Returns IP address in textual form. |
getHostName() | Returns the hostname for this IP Address. If this object was created with a hostname then it is returned, otherwise, a reverse lookup is performed to return the system configured hostname. |
getLocalHost() | Returns the IP address of the local host. |
getCanonicalHostName() | Returns the fully qualified domain name for this object. If this object was created with a hostname then it is returned, otherwise, a reverse lookup is performed to return the system configured hostname. |
hashCode() | Returns the hashcode associated with this address object. |
isAnyLocalAddress() | Returns true if this address represents a local address. |
isLinkLocalAddress() | Returns true if this address is a link-local address. |
isLoopbackAddress() | Returns true if this address is a loopback address. |
isMCGlobal() | Returns true if this multicast address has global scope. |
isMCLinkLocal() | Returns true if this multicast address has link scope. |
isMCNodeLocal() | Returns true if this multicast address has node scope. |
isMCOrgLocal() | Returns true if this multicast address has organization scope. |
isMCSiteLocal() | Returns true if this multicast address has site scope. |
isMulticastAddress() | Returns true if this address is an IP multicast address. Multicast addresses have 1110 as their first 4 bits. |
isReachable() | Returns true if this address is reachable. ICMP echo requests are used if permission can be granted otherwise the host tries to make a TCP connection at port 7 of the destination. This method is used generally as a pre-condition in various programs, to avoid Host Unreachable exceptions in the future |
isReachable() | Specify the network interface to be used while checking for reachability and the ttl parameter specifies the number of hops the echo packet makes before exiting the network. |
isSiteLocalAddress() | Returns true if this address is a site-local address. |
toString() | Converts the IP address to the string. It returns the result as hostname / IP address. |
Class Inet6Address
This class represents an Internet Protocol version 6 (IPv6) address. Defined by RFC 2373: IP Version 6 Addressing Architecture.
Textual representation of IP addresses
- The preferred form is x:x:x:x:x:x:x:x, where the ‘x’s are the hexadecimal values of the eight 16-bit pieces of the address. This is the full form. For example,
Note that it is not necessary to write the leading zeros in an individual field. However, there must be at least one numeral in every field, except as described below.
where «::FFFF:d.d.d.d» and «::d.d.d.d» are, respectively, the general forms of an IPv4-mapped IPv6 address and an IPv4-compatible IPv6 address. Note that the IPv4 portion must be in the «d.d.d.d» form. The following forms are invalid:
is valid, however it is an unconventional representation of the IPv4-compatible IPv6 address,
while «::d» corresponds to the general IPv6 address «0:0:0:0:0:0:0:d».
For methods that return a textual representation as output value, the full form is used. Inet6Address will return the full form because it is unambiguous when used in combination with other textual data.
Special IPv6 address
Description of IPv4-mapped address
IPv4-mapped address Of the form ::ffff:w.x.y.z, this IPv6 address is used to represent an IPv4 address. It allows the native program to use the same address data structure and also the same socket when communicating with both IPv4 and IPv6 nodes. In InetAddress and Inet6Address, it is used for internal representation; it has no functional role. Java will never return an IPv4-mapped address. These classes can take an IPv4-mapped address as input, both in byte array and text representation. However, it will be converted into an IPv4 address.
Textual representation of IPv6 scoped addresses
The textual representation of IPv6 addresses as described above can be extended to specify IPv6 scoped addresses. This extension to the basic addressing architecture is described in [draft-ietf-ipngwg-scoping-arch-04.txt].
Because link-local and site-local addresses are non-global, it is possible that different hosts may have the same destination address and may be reachable through different interfaces on the same originating system. In this case, the originating system is said to be connected to multiple zones of the same scope. In order to disambiguate which is the intended destination zone, it is possible to append a zone identifier (or scope_id) to an IPv6 address.
The general format for specifying the scope_id is the following:
- As a numeric identifier. This must be a positive integer that identifies the particular interface and scope as understood by the system. Usually, the numeric values can be determined through administration tools on the system. Each interface may have multiple values, one for each scope. If the scope is unspecified, then the default value used is zero.
- As a string. This must be the exact string that is returned by NetworkInterface.getName() for the particular interface in question. When an Inet6Address is created in this way, the numeric scope-id is determined at the time the object is created by querying the relevant NetworkInterface.
Note also, that the numeric scope_id can be retrieved from Inet6Address instances returned from the NetworkInterface class. This can be used to find out the current scope ids configured on the system.