0% found this document useful (0 votes)
9 views13 pages

InetAddress Class Methods Overview

The document describes a Java program that demonstrates various methods of the InetAddress class for network address manipulation. Users can retrieve local host information, resolve hostnames to IP addresses, check reachability, and explore all factory methods available in the InetAddress class. The program features a menu-driven interface allowing users to select different functionalities related to network addresses.

Uploaded by

Yashraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

InetAddress Class Methods Overview

The document describes a Java program that demonstrates various methods of the InetAddress class for network address manipulation. Users can retrieve local host information, resolve hostnames to IP addresses, check reachability, and explore all factory methods available in the InetAddress class. The program features a menu-driven interface allowing users to select different functionalities related to network addresses.

Uploaded by

Yashraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

InetAddress class Methods :-

import [Link].*;
import [Link];
import [Link];

public class InetAddressDemo {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

[Link]("============================================");
[Link](" InetAddress Class Demonstration");
[Link]("============================================");

boolean running = true;

while (running) {
[Link]("\n=== Main Menu ===");
[Link]("1. Get Local Host Information");
[Link]("2. Get IP Address by Hostname");
[Link]("3. Get Hostname by IP Address");
[Link]("4. Get All IP Addresses for a Hostname");
[Link]("5. Check if IP is Reachable");
[Link]("6. Demonstrate All Factory Methods");
[Link]("7. Exit");
[Link]("Choose an option (1-7): ");

int choice;
try {
choice = [Link]([Link]());
} catch (NumberFormatException e) {
[Link]("Invalid input! Please enter a number between 1-7.");
continue;
}

switch (choice) {
case 1:
demonstrateLocalHost();
break;
case 2:
getByHostname(scanner);
break;
case 3:
getByIPAddress(scanner);
break;
case 4:
getAllAddresses(scanner);
break;
case 5:
checkReachability(scanner);
break;
case 6:
demonstrateAllMethods();
break;
case 7:
running = false;
[Link]("Exiting program. Goodbye!");
break;
default:
[Link]("Invalid choice! Please try again.");
}
}

[Link]();
}

private static void demonstrateLocalHost() {


[Link]("\n--- Local Host Information ---");

try {
// getLocalHost() - returns InetAddress of local host
InetAddress localHost = [Link]();

[Link]("Local Host Name: " + [Link]());


[Link]("Canonical Host Name: " + [Link]());
[Link]("IP Address: " + [Link]());
[Link]("Is Loopback Address: " + [Link]());
[Link]("Is Site Local Address: " + [Link]());
[Link]("Is Link Local Address: " + [Link]());
[Link]("Is Multicast Address: " + [Link]());

// getLoopbackAddress() - returns loopback address ([Link])


InetAddress loopback = [Link]();
[Link]("Loopback Address: " + [Link]());

} catch (UnknownHostException e) {
[Link]("Error: Could not determine local host information.");
[Link]("Exception: " + [Link]());
}
}
private static void getByHostname(Scanner scanner) {
[Link]("\n--- Get IP Address by Hostname ---");
[Link]("Enter hostname (e.g., [Link], [Link]): ");
String hostname = [Link]().trim();

if ([Link]()) {
[Link]("Hostname cannot be empty!");
return;
}

try {
// getByName() - returns InetAddress for the given hostname
InetAddress address = [Link](hostname);

[Link]("\nResults for '" + hostname + "':");


[Link]("Host Name: " + [Link]());
[Link]("Canonical Host Name: " + [Link]());
[Link]("IP Address: " + [Link]());
[Link]("Is Reachable (5s timeout): " + [Link](5000));

} catch (UnknownHostException e) {
[Link]("Error: Unknown host '" + hostname + "'");
[Link]("Exception: " + [Link]());
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}

private static void getByIPAddress(Scanner scanner) {


[Link]("\n--- Get Hostname by IP Address ---");
[Link]("Enter IP address (e.g., [Link], [Link]): ");
String ipAddress = [Link]().trim();

if ([Link]()) {
[Link]("IP address cannot be empty!");
return;
}

try {
// getByName() with IP address - performs reverse lookup
InetAddress address = [Link](ipAddress);

[Link]("\nResults for IP '" + ipAddress + "':");


[Link]("Host Name: " + [Link]());
[Link]("Canonical Host Name: " + [Link]());
[Link]("IP Address: " + [Link]());
[Link]("Is Reachable (5s timeout): " + [Link](5000));

} catch (UnknownHostException e) {
[Link]("Error: Unknown IP address '" + ipAddress + "'");
[Link]("Exception: " + [Link]());
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}

private static void getAllAddresses(Scanner scanner) {


[Link]("\n--- Get All IP Addresses for a Hostname ---");
[Link]("Enter hostname (e.g., [Link], [Link]): ");
String hostname = [Link]().trim();

if ([Link]()) {
[Link]("Hostname cannot be empty!");
return;
}

try {
// getAllByName() - returns all IP addresses for the given hostname
InetAddress[] allAddresses = [Link](hostname);

[Link]("\nAll IP addresses for '" + hostname + "':");


for (int i = 0; i < [Link]; i++) {
InetAddress addr = allAddresses[i];
[Link]((i + 1) + ". " + [Link]() +
" (Hostname: " + [Link]() + ")");
}

} catch (UnknownHostException e) {
[Link]("Error: Unknown host '" + hostname + "'");
[Link]("Exception: " + [Link]());
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}

private static void checkReachability(Scanner scanner) {


[Link]("\n--- Check Host Reachability ---");
[Link]("Enter hostname or IP address: ");
String input = [Link]().trim();
if ([Link]()) {
[Link]("Input cannot be empty!");
return;
}

try {
InetAddress address = [Link](input);

[Link]("\nTesting reachability of: " + [Link]());


[Link]("Hostname: " + [Link]());

// isReachable() - tests if the address is reachable


boolean isReachable = [Link](5000); // 5 second timeout

[Link]("Reachable: " + isReachable);


[Link]("Is Loopback: " + [Link]());
[Link]("Is Site Local: " + [Link]());

} catch (UnknownHostException e) {
[Link]("Error: Unknown host '" + input + "'");
[Link]("Exception: " + [Link]());
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}

private static void demonstrateAllMethods() {


[Link]("\n--- Demonstrating All InetAddress Factory Methods ---");
try {
// 1. getLocalHost()
[Link]("\n1. getLocalHost():");
InetAddress localHost = [Link]();
[Link](" " + localHost);

// 2. getLoopbackAddress()
[Link]("\n2. getLoopbackAddress():");
InetAddress loopback = [Link]();
[Link](" " + loopback);

// 3. getByName() with hostname


[Link]("\n3. getByName() with hostname:");
InetAddress google = [Link]("[Link]");
[Link](" [Link] -> " + [Link]());

// 4. getByName() with IP address


[Link]("\n4. getByName() with IP address:");
InetAddress byIP = [Link]("[Link]");
[Link](" [Link] -> " + [Link]());

// 5. getAllByName() - multiple addresses


[Link]("\n5. getAllByName() with [Link]:");
InetAddress[] allGoogle = [Link]("[Link]");
for (int i = 0; i < [Link]([Link], 3); i++) {
[Link](" " + (i + 1) + ". " + allGoogle[i].getHostAddress());
}
if ([Link] > 3) {
[Link](" ... and " + ([Link] - 3) + " more addresses");
}
// 6. getByAddress() with byte array
[Link]("\n6. getByAddress() with byte array:");
byte[] ipBytes = {(byte) 8, (byte) 8, (byte) 8, (byte) 8};
InetAddress byBytes = [Link](ipBytes);
[Link](" byte[8,8,8,8] -> " + [Link]());

// 7. getByAddress() with hostname and byte array


[Link]("\n7. getByAddress() with hostname and byte array:");
InetAddress byBytesWithName = [Link]("[Link]", ipBytes);
[Link](" [Link] -> " + [Link]());

// Demonstrate instance methods


[Link]("\n--- Instance Methods Demonstration ---");
[Link]("Local Host Name: " + [Link]());
[Link]("Local Canonical Name: " + [Link]());
[Link]("Local IP Address: " + [Link]());
[Link]("Local isLoopback: " + [Link]());
[Link]("Google isReachable: " + [Link](3000));

} catch (UnknownHostException e) {
[Link]("UnknownHostException: " + [Link]());
} catch (Exception e) {
[Link]("Exception: " + [Link]());
[Link]();
}
}

// Utility method to display byte array as IP address


private static String bytesToIp(byte[] bytes) {
if (bytes == null) return "null";

StringBuilder sb = new StringBuilder();


for (int i = 0; i < [Link]; i++) {
[Link](bytes[i] & 0xFF);
if (i < [Link] - 1) {
[Link](".");
}
}
return [Link]();
}
}

Output:-
1)

2)
3)

4)

5)
6)

7)

You might also like