Lap DC 1
Lap DC 1
InetAddress
It uses to find either the domain name, the IP address(es), or both that are
associated together, along with a number of functions that responsible on examine
the reachability, comparison, and so on.
InetAddress represents an Internet Protocol (IP) address. An instance of an
InetAddress consists of an IP address and possibly its corresponding host name
(depending on whether it is constructed with a host name or whether it has already
done reverse host name resolution). The used functions when you create object are
as follow:
1. getAllByName(String host) Given the name of a host, returns an array
of its IP addresses, based on the configured name service on the system.
The object must be defined as an array of InetAddress.
2. getByName(String host) Determines the IP address of a host depends
on given the host's name.
3. getLoopbackAddress() Returns the machine loopback address which
is (127.0.0.1).
4. getLocalHost() Returns the address of the local host which is the
machine name and IP address.
In terms of printing or depends on the questions, you can extract the data that you
got from above functions using the following functions:
1. getHostAddress() Returns the IP address.
2. getHostName() Returns the host name (domain name).
3. equals(Object obj) Compares this object against another specified
object.
4. isLoopbackAddress() Check if the InetAddress is a loopback address.
5. isMulticastAddress() Check if the InetAddress is an IP multicast
address. (Class D ) (Range 224.0.0.0 - 239.255.255.255).
6. isReachable(int timeout) Test whether that address is reachable same
as doing “ping”. Timeout in millisecond (1000 millisecond =1 second).
7. toString() Converts IP address or any information if they are not string
into a String.
8. getClass Returns the runtime class of this Object which means the class
name, along with the name of package. In this class, you can find the
version of IP if it is 4 or 6 depends on the package name. the syntax of
IPv4 is x.x.x.x and IPv6 is x:x:x:x:x:x:x:x ex:
a. IPv4 192.168.2.1
b. IPv6 0:0:0:0:0:0:0:1
1|P ag e By: Ghasaq Bahaa
Data Communication & Networking I week 1 Practical Lecture
Code:
Note: try-catch and throws are used to catch an exception in terms of:
1. The host name or the IP address is not exist or a syntax error, the exception
called (UnknownHostException).
2. IOException is the general class of exceptions produced by failed or
interrupted I/O operations. You need to use it if you use isReachable(int)
function.
References:
https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html