Networking in Java | Set 1 (Java.net.InetAddress class)

Course Curriculum

Networking in Java | Set 1 (Java.net.InetAddress class)

Networking in Java | Set 1 (Java.net.InetAddress class)

  • This class represents an IP address. It represents both the 32 bit IPv4 address and 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.
  • There are no constructors for this class but static methods which returns instances of InetAddress class for general use.

Methods :

  • getAddress() : returns raw IP address of this InetAddress object as an array. The order in which bytes appear in array are same as in IP address i.e. getAddress[0] will contain highest order byte.
    Syntax : public byte[] getAddress()
  • getHostAddress() : returns IP address in textual form.
    Syntax :public String getHostAddress()
  • isAnyLocalAddress() : returns true if this address represents a local address.
    Syntax :public boolean isAnyLocalAddress()
  • isLinkLocalAddress() : returns true if this address is a link local address.
    Syntax :public boolean isLinkLocalAddress()
  • isLoopbackAddress() : returns true if this address is a loopback address.
    Syntax :public boolean isLoopbackAddress()
  • isMCGlobal() : returns true if this multicast address has global scope.
    Syntax :public boolean isMCGloabal()
  • isMCLinkLocal() : returns true if this multicast address has link scope.
    Syntax :public boolean isMCLinkLocal()
  • isMCNodeLocal() : returns true if this multicast address has node scope.
    Syntax :public boolean isMCNodeLocal()
  • isMCOrgLocal() : returns true if this multicast address has organisation scope.
    Syntax :public boolean isMCOrgLoacal()
  • isMCSiteLocal() : returns true if this multicast address has site scope.
    Syntax :public boolean isMCSiteLocal()
  • isMulticastAddress() : returns true if this address is an IP multicast address. Multicast addresses have 1110 as their first 4 bits.
    Syntax :public boolean isMulticastAddress()
  • isSiteLocalAddress(): returns true if this address is a site local address.
    Syntax :public boolean isSiteLocalAddress()()
  • hashCode() : returns the hashcode associated with this address object.
    Syntax : public int hashCode()
  • equals() : returns true if this ip address is same as that of the object specified. Equals() method don’t consider host names while comparing and only consider IP address associated.
    Syntax : public boolean equals(Object obj)
    Parameters :
    obj : object to compare with

// Java program to illustrate
// Inetaddress class methods
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class inetadd
{

public static void main(String[] args) throws UnknownHostException
{

String url = "www.prutor.ai";
byte addr[]={127, 0, 0, 1};
InetAddress ip1 = Inet4Address.getByName(url);
InetAddress ip2 = InetAddress.getByAddress(addr);

// Following methods checks the property of the thus created object.
// getAddress() method
System.out.println("Address : " + Arrays.toString(ip1.getAddress()));

// getHostAddress() method
System.out.println("Host Address : " + ip1.getHostAddress());

// isAnyLocalAddress() method
System.out.println("isAnyLocalAddress : " + ip1.isAnyLocalAddress());

// isLinkLocalAddress() method
System.out.println("isLinkLocalAddress : " + ip1.isLinkLocalAddress());

// isLoopbackAddress() method
System.out.println("isLoopbackAddress : " + ip1.isLoopbackAddress());

// isMCGlobal() method
System.out.println("isMCGlobal : " + ip1.isMCGlobal());

// isMCLinkLocal() method
System.out.println("isMCLinkLocal : " + ip1.isMCLinkLocal());

// isMCNodeLocal() method
System.out.println("isMCNodeLocal : " + ip1.isMCNodeLocal());

// isMCOrgLocal() method
System.out.println("isMCOrgLocal : " + ip1.isMCOrgLocal());

// isMCSiteLocal() method
System.out.println("isMCSiteLocal : " + ip1.isMCSiteLocal());

// isMulticastAddress() method
System.out.println("isMulticastAddress : " + ip1.isMulticastAddress());

// isSiteLocalAddress() method
System.out.println("isSiteLocalAddress : " + ip1.isSiteLocalAddress());

// hashCode() method
System.out.println("hashCode : " + ip1.hashCode());

// equals() method
System.out.println("ip1==ip2 : " + ip1.equals(ip2));
}

}
Output :

Address : [52, 84, 102, 90]
Host Address : 52.84.102.90
isAnyLocalAddress : false
isLinkLocalAddress : false
isLoopbackAddress : false
isMCGlobal : false
isMCLinkLocal : false
isMCNodeLocal : false
isMCOrgLocal : false
isMCSiteLocal : false
isMulticastAddress : false
isSiteLocalAddress : false
hashCode : 877946458
ip1==ip2 : false

  • 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 future.
    Syntax :public boolean isReachable(int timeout)
    throws IOException
    Parameters :
    timeout : time after which the call aborts, resulting in false value.
    Throws :
    IOException : if network error occurs
  • Another overloaded isReachable() method 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.

Syntax :public boolean isReachable(NetworkInterface netif,

int ttl,
int timeout)
throws IOException
Parameters :
netif : Network interface to use
ttl : time to live in milliseconds
timeout : time after which the call aborts, resulting in false value.
Throws :
IOException : if network error occurs

  • getHostName() : Returns the host name for this IP Address. If this object was created with a host name than it is returned, otherwise a reverse lookup is performed to return the system configured host name.
    Syntax :public String getHostName()
  • getCanonicalHostName() : Returns the fully qualified domain name for this object. If this object was created with a host name than it is returned, otherwise a reverse lookup is performed to return the system configured host name.
    Syntax :public String getCanonicalHostName()
  • toString() : Converts the IP address to the string. It returns the result as hostname / IP address.
    Syntax :public String toString()
  • getByAddress() : One of the static methods to create an InetAddress object. It takes host name and IP address as its parameter. Host name can be the machine name as in “www.prutor.ai” or its textual IP address.
    Syntax : public static InetAddress getByAddress(String host,
    byte[] addr)
    throws UnknownHostException
    Parameters :
    host : hostname
    addr : byte address for this object
    Throws :
    UnknownHostException : if IP address is of illegal length
    Another overloaded method which only takes byte address and no host name. It returns the InetAddress object with this raw IP address only.

Syntax : public static InetAddress getByAddress(byte[] addr)
throws UnknownHostException
Parameters :
host : hostname
addr : byte address for this object
Throws :
UnknownHostException : if IP address is of illegal length

  • getByName() : Returns the IP Address of the host specified. If the host is a literal IP address, then only its validity is checked.
    Syntax :public static InetAddress getByName(String host)
    Parameters :
    host : host name
  • getAllByName() : Returns an array of IP addresses for the given host.
    Syntax :public static InetAddress[] getAllByName(String host)
    Parameters :
    host : host name
  • getLoopbackAddress() : Returns the loopback address.
    Syntax :public static InetAddress getLoopbackAddress()
  • getLocalHost() : Returns the IP address of the local host.
    Syntax :public static InetAddress getLocalHost()
    throws UnknownHostException
    Throws :
    UnknownHostException : If local host cannot be resolved into an address

// Java program to illustrate
// Inetaddress class methods
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;

public class inetadd2
{
public static void main(String[] args) throws IOException
{

String url = "www.prutor.ai";
byte addr[] = { 127, 0, 0, 1 };

// getByName() method
InetAddress ip1 = InetAddress.getByName(url);
System.out.println("getByName() : " + ip1);

// getByAddress() method
InetAddress ip2 = InetAddress.getByAddress(addr);
System.out.println("getByAddress() : " + ip2);

// getLocalHost() method
InetAddress ip3 = InetAddress.getLocalHost();
System.out.println("getLocalHost() : " + ip3);

// getLoopbackAddress() method
InetAddress ip4 = InetAddress.getLoopbackAddress();
System.out.println("getLoopbackAddress() : " + ip4);

// getAllByName() method - returns all ip addresses
// associated with google.com
InetAddress addrs[] = InetAddress.getAllByName("www.google.com");
System.out.println("Google ip addresses : " + Arrays.toString(addrs));

// isReachable() method
boolean isreach = ip1.isReachable(50);
System.out.println("ip1 isReachable() : " + isreach);

// getHostname() method
String hostname = ip1.getHostName();
System.out.println("ip1 hostname :" + hostname);

// getCanonicalHostname() method
System.out.println("ip1 CanonicalHostname : " + ip1.getCanonicalHostName());

// toString() method
System.out.println("ip1 toString() : " + ip1.toString());
}
}
Output :

getByName() : www.prutor.ai/52.84.102.90
getByAddress() : /127.0.0.1
getLocalHost() : DESKTOP-K4GGDH6/192.168.1.5
getLoopbackAddress() : localhost/127.0.0.1
Google ip addresses : [www.google.com/216.58.199.132]
ip1 isReachable() : false
ip1 hostname :www.prutor.ai
ip1 CanonicalHostname : server-52-84-102-90.del51.r.cloudfront.net
ip1 toString() : www.prutor.ai/52.84.102.90

The following program uses InetAddress class to get IP address of the given domain name. When the program is run on a system connected to the Internet, it gives the IP address(es) of the domain given.

// A Java program to demonstrate working of InetAddress class
// This program finds IP address for a domain name.
import java.net.*;

public class GetIPAddress
{
public static void main(String args[]) throws Exception
{
String url = "www.google.com";
try
{
// Get IP addresses related to the domain
InetAddress ips[] = InetAddress.getAllByName(url);

// Display ip addresses
System.out.println("IP Address(es)");
for (InetAddress addr:ips)
System.out.println(addr.getHostAddress());
}
catch(Exception ex)
{
System.out.println("host not found");
}
}
}
Result :

IP Address(es)
172.217.4.68
2607:f8b0:4006:809:0:0:0:2004

(Next Lesson) How to start learning Java