Instructional Objectives
After participating in this practicum, students are expected to be able to:
- Understanding internet address class
- Create a program to implement internet address class.
1. Internet Address
The java.net.InetAddress class is a high-level Java representation of an IP address, both IPv4 and IPv6. It is used by most other networking classes, including Socket, ServerSocket, URL, DatagramSocket, DatagramPacket, and more. Typically, it includes a hostname and an IP address.
InetAddress has three static methods that return according to the initialization of the object. These methods are:
- public static InetAddress getByName(String hostName) throws UnknownHostException
- public static InetAddress[] getAllByName(String hostName) throws UnknownHostException
- public static InetAddress getLocalHost( ) throws UnknownHostException.
These three methods can make connections to local DNS servers to populate information in InetAddress objects. The other methods in this class, such as getAddress() and getHostName(), mostly work with information provided by one of the three methods.
2. Example Program
import java.net.*;
class myAddress {
public static void main (String args[]) {
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println(address);
} catch (UnknownHostException e) {
System.out.println("Could not find this computer's address.");
}
}
}