Introduction
The java.net package includes several classes and interfaces, which provides support for networking. These classes encapsulate the “Socket” paradigm. Java supports TCP/IP in two ways.
- By extending the stream I/O interface
- By adding features required to build I/O Objects across the network.
Java supports both TCP/IP and UDP families of protocol. TCP is used for reliable stream-based I/O across the network whereas UDP supports a point-to-point datagram-oriented model.
The InetAddress Class
Whenever you want to establish a connection between two machines across the Internet or any other kind of network, addresses are fundamental and the InetAddress class is used to convert the Domain name (for textual Internet Address) of an Internet address into an object, which represents that address. The InetAddress class does not have any constructors but it has three factory methods that can be used to create instances of the InetAddress class.
Note. According to Object-Oriented Programming, the term factory applies to a class that can construct instances of that class without invoking constructors. The instances of the class are constructed through static methods built into the class.
The following are the factory methods of the InetAddress class.
- static InetAddress getLocalHost() throws UnknownHostException
- static InetAddressgetByName(String hostName) throws UnknownHostException
- static InetAddress [] getAllByName(String hostname) throws UnknownHostException
The getLocalHost() method returns the InetAddress object that represents the local host.
The getByName(String hostName) method takes the hostname as a parameter and returns the InetAddress object.
The getAllByName(String Hostname) method returns an array of InetAddress objects that include all the addresses to which a particular name(passed as a parameter to the method) resolves.
Note. All the above-mentioned factory methods throw UnknownHostException if the methods cannot resolve the hostname. The program will display the name and address of the local machine.
import java.net.*;
class ObtainingIP {
public static void main(String args[]) throws UnknownHostException {
InetAddress adr;
adr = InetAddress.getLocalHost();
System.out.println("The info about your machine is: " + adr);
}
}
Output
The output shows the IP address and the host name for the local host.
Another illustration represents that displays the name and address of any machine on network.
import java.io.*;
import java.net.*;
class ObtainingIP2 {
public static void main(String args[]) throws UnknownHostException, IOException {
InetAddress adr;
String host;
BufferedReader str = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the machine's host name: ");
host = str.readLine();
adr = InetAddress.getByName(host);
System.out.println("The info about the host " + host + " is " + adr);
}
}
Output
Note. BufferedReader and InputStreamReader are defined in the java.io package. The BufferedReader class includes methods that allow you to read text lines from a character input stream. Since the Standard input (System.in) is a byte stream, the InputStreamReader class converts the byte stream of the standard input, System.in, into a character stream. The method readLine() allows you to read a text line from an input stream. For example, Obtaining all IP addresses of a host.
import java.io.*;
import java.net.*;
class ObtainingAll {
public static void main(String args[]) throws UnknownHostException, IOException {
InetAddress adr[];
String host;
BufferedReader str = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Web site address (DNS): ");
host = str.readLine();
adr = InetAddress.getAllByName(host);
for (int i = 0; i < adr.length; i++) {
System.out.println(adr[i]);
}
}
}
Output
The InetAddress class also defines some non-static methods, which can be used on objects returned by the factory methods.
Methods Used
- String getHostName(): Returns the string representation of the hostname associated with the InetAddress object.
- byte[] getAddress(): Returns an array of four bytes representing the numeric address of the host. This array of bytes cannot be printed directly by casting them to int. They have to be copied into an array of integers while ANDing with 255 to undo sing-extending
- String toString(): Returns a string that consists of the hostname and the IP address. For Example “Ashish/135.147.167.2”. This function can be overridden.
- boolean equals(InetAddress addr): Returns true if the invoking object has the same internet address as addr.
Let us consider an example that provided the information about the local host i,e the machine on which you run the program. Usage of the getAddress() and overriding of the toString() method to display the IP address.
import java.net.*;
class DemoInetAddress4 {
public static void main(String arg[]) throws UnknownHostException {
Demo d = new Demo();
System.out.println(d);
}
}
class Demo {
static InetAddress adr;
byte[] numAdr;
String AddrMsg;
Demo() throws UnknownHostException {
adr = InetAddress.getLocalHost();
numAdr = adr.getAddress();
AddrMsg = adr.getHostName() + "/";
}
public String toString() {
for (int i = 0; i < numAdr.length; i++) {
AddrMsg += (numAdr[i] & 255) + ".";
}
return "The Local Machine's Address is: " + AddrMsg;
}
}
Output
When you print an instance of a class with System.out.print or System.out.println , the println method simplicity invokes the toString() method to print the message associated with the object. Hence the argument name d here is resolved to d.toString().
Summary
The java.net package in Java provides support for networking through classes and interfaces based on the "Socket" paradigm. It supports both TCP/IP for reliable stream-based I/O and UDP for point-to-point datagram communication. The InetAddress class is crucial for handling internet addresses, and converting domain names into objects representing those addresses. This class provides factory methods like getLocalHost(), getByName(), and getAllByName() to obtain IP addresses. Several code examples demonstrate how to retrieve and display the IP address and hostname of local and remote machines. Additionally, methods like getHostName(), getAddress(), and toString() allow further manipulation and retrieval of host details.