Network Programming – Java InetAddress Class

Home/Java - Tutorial/Network Programming/Network Programming – Java InetAddress Class
Network Programming – Java InetAddress Class 2017-06-04T00:02:38+00:00

Java InetAddress Class

Java InetAddress Class is used to encapsulate the two thing.

1. Numeric IP Address 
2. The domain name for that address.
The InetAddress can handle both IPv4 and IPv6 addressses. It has no visible constructors so to create its object, the user have to use one of the available in-built static methods.
The commonly used InetAddress in-built methods are:
(1) getLocalHost(): It returns the InetAddress object that represents the local host contain the name and address both. If this method unable to find out the host name, it throw an UnknownHostException.
Syntax:
Static  InetAddress getLocalHost() throws UnknownHostException
(2) getByName(): It returns an InetAddress for a host name passed to it as a parameter argument. If this method unable to find out the host name, it throw an UnknownHostException.
Syntax:
Static  InetAddress getByName(String host_name) throws UnknownHostException
(3) getAllByName(): It returns an array of an InetAddress that represent all of the addresses that a particular name resolve to it. If this method can’t find out the name to at least one address, it throw an UnknownHostException.
Syntax:
Static  InetAddress[] getAllByName(String host_name) throws UnknownHostException
Program: Write down a program which demonstrate an InetAddress class.

 

Output:

HOST NAME – Numeric Address : Ashutosh-2c89cd5e0a/127.0.0.1
HOST DEFAULT-NAME / IP : localhost/127.0.0.1
HOST IP-ADDRESS : 127.0.0.1
HOST DEFAULT-NAME : localhost

Prev Next