Vision Institute of Technology, Aligarh
[Link] Computer Science 3rd year
Computer Network Lab Manual
BCS 653
Prepared by : Dr Naveen Singh
Course Outcomes
Subject Name: Computer Networks Lab Subject code: BCS-653
The students are expected to be able to demonstrate the following knowledge, skills and attitudes
after completing this course:
1. To understand the basic concepts of network devices and connectivity.
2. To analyze network traffic using wire shark tool.
3. To design and configure a network using Cisco Packet Tracer.
4. To implement a client/server chatting program using socket programming
Program 1 Implementation of Stop and wait protocol and sliding window protocol
1. Sender Implementation:
File Name: [Link]
1. import [Link].*;
2. import [Link].*;
3. public class Sender {
4. public static void main(String[] args) throws Exception {
5. DatagramSocket socket = new DatagramSocket();
6. InetAddress receiverAddress = [Link]("localhost");
7. int receiverPort = 9876;
8. int windowSize = 4;
9. byte[] data = "Hello, Sliding Window!".getBytes();
10. int base = 0;
11. while (base < [Link]) {
12. for (int i = base; i < base + windowSize && i < [Link]; i++) {
13. DatagramPacket packet = new DatagramPacket(data, i, 1, receiverAddress, receiverPor
t);
14. [Link](packet);
15. }
16. DatagramPacket ackPacket = new DatagramPacket(new byte[1], 1);
17. [Link](ackPacket);
18. int ack = [Link]()[0];
19. if (ack >= base) {
20. base = ack + 1;
21. }
22. }
23. [Link]();
24. }
25. }
Output:
Sent: H
Sent: e
Sent: l
Sent: l
Received ACK: 0
Sent: o
Sent: ,
Sent: S
Received ACK: 1
Sent: l
Sent: i
Sent: d
Received ACK: 2
Sent: i
Sent: n
Received ACK: 3
Sent: g
Sent:
Received ACK: 4
Sent: W
Sent: i
Sent: n
Sent: d
Received ACK: 5
Sent: o
Sent: w
Sent: !
Received ACK: 6
2. Receiver Implementation:
File Name: [Link]
1. import [Link].*;
2. import [Link].*;
3. public class Receiver {
4. public static void main(String[] args) throws Exception {
5. DatagramSocket socket = new DatagramSocket(9876);
6. int expectedSeqNum = 0;
7. while (true) {
8. DatagramPacket packet = new DatagramPacket(new byte[1], 1);
9. [Link](packet);
10. int seqNum = [Link]()[0];
11. if (seqNum == expectedSeqNum) {
12. [Link]("Received: " + seqNum);
13. DatagramPacket ackPacket = new DatagramPacket(new byte[] { (byte) seqNum }, 1, p
[Link](), [Link]());
14. [Link](ackPacket);
15. expectedSeqNum++;
16. }
17. }
18. }
19. }
Output:
Received: 0
Received: 1
Received: 2
Received: 3
Received: 4
Received: 5
Received: 6
Program 2 Study of Socket programming and Client Server model
A Java program for a Client
import [Link].*;
import [Link].*;
public class Client {
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try {
socket = new Socket(address, port);
[Link]("Connected");
// takes input from terminal
input = new DataInputStream([Link]);
// sends output to the socket
out = new DataOutputStream(
[Link]());
catch (UnknownHostException u) {
[Link](u);
return;
catch (IOException i) {
[Link](i);
return;
}
// string to read message from input
String line = "";
// keep reading until "Over" is input
while () {
try {
line = [Link]();
[Link](line);
catch (IOException i) {
[Link](i);
// close the connection
try {
[Link]();
[Link]();
[Link]();
catch (IOException i) {
[Link](i);
public static void main(String args[])
Client client = new Client("[Link]", 5000);
Server Programming
Establish a Socket Connection
To write a server application two sockets are needed.
A ServerSocket which waits for the client requests (when a client makes a new Socket())
A plain old Socket to use for communication with the client.
Communication
getOutputStream() method is used to send the output through the socket.
Close the Connection
After finishing, it is important to close the connection by closing the socket as well as
input/output streams.
Java
// A Java program for a Server
import [Link].*;
import [Link].*;
public class Server
{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
// constructor with port
public Server(int port)
// starts server and waits for a connection
try
server = new ServerSocket(port);
[Link]("Server started");
[Link]("Waiting for a client ...");
socket = [Link]();
[Link]("Client accepted");
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream([Link]()));
String line = "";
// reads message from client until "Over" is sent
while ()
try
line = [Link]();
[Link](line);
catch(IOException i)
[Link](i);
[Link]("Closing connection");
// close connection
[Link]();
[Link]();
catch(IOException i)
[Link](i);
public static void main(String args[])
Server server = new Server(5000);
Important Points
Server application makes a ServerSocket on a specific port which is 5000. This starts our
Server listening for client requests coming in for port 5000.
Then Server makes a new Socket to communicate with the client.
socket = [Link]()
The accept() method blocks(just sits there) until a client connects to the server.
Then we take input from the socket using getInputStream() method. Our Server keeps
receiving messages until the Client sends “Over”.
After we’re done we close the connection by closing the socket and the input stream.
To run the Client and Server application on your machine, compile both of them. Then first
run the server application and then run the Client application.
To run on Terminal or Command Prompt
Open two windows one for Server and another for Client
1. First run the Server application as,
$ java Server
Server started
Waiting for a client …
2. Then run the Client application on another terminal as,
$ java Client
It will show – Connected and the server accepts the client and shows,
Client accepted
3. Then you can start typing messages in the Client window. Here is a sample input to the
Client
Hello
I made my first socket connection
Over
Which the Server simultaneously receives and shows,
Hello
I made my first socket connection
Over
Closing connection
Notice that sending “Over” closes the connection between the Client and the Server just like
said before.
If you’re using Eclipse or likes of such-
1. Compile both of them on two different terminals or tabs
2. Run the Server program first
3. Then run the Client program
4. Type messages in the Client Window which will be received and shown by the Server
Window simultaneously.
5. Type Over to end.
Program 3 Write a code simulating ARP and RARP protocols
ALGORITHM:
Client
1. Start the program
2. Using socket connection is established between client and server.
3. Get the IP address to be converted into MAC address.
4. Send this IP address to server.
5. Server returns the MAC address to client.
Server
1. Start the program
2. Accept the socket which is created by the client.
3. Server maintains the table in which IP and corresponding MAC addresses are stored.
4. Read the IP address which is send by the client.
5. Map the IP address with its MAC address and return the MAC address to client.
Progra
m
Client:
import [Link].*;
import [Link].*;
import [Link].*;
class Clientarp
{
public static void main(String args[])
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader([Link]));
Socket clsct=new Socket("[Link]",5604);
DataInputStream din=new DataInputStream([Link]());
DataOutputStream dout=new
DataOutputStream([Link]());
[Link]("Enter the Logical address(IP):");
String str1=[Link]();
[Link](str1+'\n');
String str=[Link]();
[Link]("The Physical Address is: "+str);
[Link]();
}
catch (Exception e)
{
[Link](e);
}
}
}
Server:
import [Link].*;
import
[Link].*;
import
[Link].*;
class Serverarp
{
public static void main(String args[])
{
try
{
ServerSocket obj=new
ServerSocket(5604);
Socket obj1=[Link]();
while(true)
{
DataInputStream din=new DataInputStream([Link]());
DataOutputStream dout=new
DataOutputStream([Link]()); String str=[Link]();
String ip[]={"[Link]","[Link]"};
String mac[]={"[Link]","[Link]"};
for(int i=0;i<[Link];i++)
{ if([Link](ip[i]))
{
[Link](mac[i]+'\n');
break;
}
}
[Link]();
}
}
catch(Exception e)
{
[Link](e);
}
}
}
Output:
E:\networks>java Serverarp
E:\networks>java Clientarp
Enter the Logical address(IP):
[Link]
The Physical Address is: [Link]
Result: Thus the ARP protocol using TCP Sockets program was executed.
Program 4 To write a java program for simulating RARP protocols using UDP
ALGORITHM
Client
1. Start the program
2. using datagram sockets UDP function is established.
2. Get the MAC address to be converted into IP address.
3. Send this MAC address to server.
4. Server returns the IP address to client.
Server
1. Start the program.
2. Server maintains the table in which IP and corresponding MAC addresses are stored.
3. Read the MAC address which is send by the client.
4. Map the IP address with its MAC address and return the IP address to client.
Client:
import [Link].*;
import [Link].*;
import [Link].*;
class Clientrarp
{
public static void main(String args[])
{
try
{
DatagramSocket client=new DatagramSocket();
InetAddress addr=[Link]("[Link]");
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
BufferedReader in=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the Physical address (MAC):");
String str=[Link](); sendbyte=[Link]();
DatagramPacket sender=new DatagramPacket(sendbyte,[Link],addr,1309);
[Link](sender);
DatagramPacket receiver=new DatagramPacket(receivebyte,[Link]);
[Link](receiver);
String s=new String([Link]());
[Link]("The Logical Address is(IP): "+[Link]());
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
Server:
import [Link].*;
import [Link].*;
import [Link].*;
class Serverrarp
{
public static void main(String args[])
{
try
{
DatagramSocket server=new DatagramSocket(1309);
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
DatagramPacket receiver=new DatagramPacket(receivebyte,[Link]);
[Link](receiver);
String str=new String([Link]());
String s=[Link]();
InetAddress addr=[Link]();
int port=[Link]();
String ip[]={"[Link]","[Link]"};
String mac[]={"[Link]","[Link]"};
for(int i=0;i<[Link];i++)
{
if([Link](mac[i]))
{
sendbyte=ip[i].getBytes();
DatagramPacket sender=new DatagramPacket(sendbyte,[Link],addr,port);
[Link](sender); break;
}
}
break;
}
}
catch(Exception e)
{
[Link](e);
}
}
}
Output:
I:\ex>java Serverrarp12 I:\ex>java
Clientrarp12
Enter the Physical address (MAC):
[Link]
The Logical Address is(IP): [Link]
Result : Thus the RARP program using UDP was executed.
Program 5
Remote Procedure Call (RPC) in Java using Java RMI (Remote Method Invocation), which is
Java's built-in support for RPC-style communication.
1. [Link] – Remote Interface
java
CopyEdit
import [Link];
import [Link];
public interface Adder extends Remote {
int add(int x, int y) throws RemoteException;
}
2. [Link] – Implementation of the Interface
java
CopyEdit
import [Link];
import [Link];
public class AdderImpl extends UnicastRemoteObject implements Adder {
protected AdderImpl() throws RemoteException {
super();
}
public int add(int x, int y) throws RemoteException {
return x + y;
}
}
3. [Link] – Registering the Remote Object
java
CopyEdit
import [Link];
import [Link];
public class Server {
public static void main(String[] args) {
try {
[Link](1099); // Start RMI registry on port 1099
Adder adder = new AdderImpl();
[Link]("rmi://localhost
Program 6
Enter IP address: [Link]
Enter number of subnets: 4
Java Program – Subnetting Implementation
import [Link];
public class Subnetting {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter IP address (e.g., [Link]): ");
String ip = [Link]();
[Link]("Enter number of subnets: ");
int subnets = [Link]();
String[] parts = [Link]("\\.");
int[] ipParts = new int[4];
for (int i = 0; i < 4; i++) {
ipParts[i] = [Link](parts[i]);
}
// Subnet calculation (for Class C)
int borrowedBits = (int) [Link]([Link](subnets) / [Link](2));
int subnetMask = 256 - (int) [Link](2, 8 - borrowedBits);
int totalSubnets = (int) [Link](2, borrowedBits);
int hostsPerSubnet = (int) [Link](2, 8 - borrowedBits) - 2;
[Link]("\nBorrowed bits: " + borrowedBits);
[Link]("Subnet mask: 255.255.255." + subnetMask);
[Link]("Total subnets: " + totalSubnets);
[Link]("Hosts per subnet: " + hostsPerSubnet);
int increment = 256 / totalSubnets;
for (int i = 0; i < totalSubnets; i++) {
int subnetStart = i * increment;
int subnetEnd = subnetStart + increment - 1;
[Link]("\nSubnet " + (i + 1) + ":");
[Link]("Network Address: " + ipParts[0] + "." + ipParts[1] + "." + ipParts[2] +
"." + subnetStart);
[Link]("First Host: " + ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + "." +
(subnetStart + 1));
[Link]("Last Host: " + ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + "." +
(subnetEnd - 1));
[Link]("Broadcast Address: " + ipParts[0] + "." + ipParts[1] + "." + ipParts[2] +
"." + subnetEnd);
}
[Link]();
}
}
Sample Output:
yaml
Copy
Edit
Enter IP address (e.g., [Link]): [Link]
Enter number of subnets: 4
Borrowed bits: 2
Subnet mask: [Link]
Total subnets: 4
Hosts per subnet: 62
Subnet 1:
Network Address: [Link]
First Host: [Link]
Last Host: [Link]
Broadcast Address: [Link]
Subnet 2:
Network Address: [Link]
First Host: [Link]
Last Host: [Link]
Broadcast Address: [Link]
...
Program 7
Applications using TCP and UDP Sockets like a. DNS b. SNMP c. File Transfer in JAVA
a) DNS Client using UDP Socket in Java
import [Link].*;
public class DNSClient {
public static void main(String[] args) {
try {
String domain = "[Link]";
InetAddress ip = [Link](domain);
[Link]("IP address of " + domain + " is: " + [Link]());
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
☑ This uses Java's built-in DNS resolution (UDP under the hood).
✅(b) SNMP-Like Simulation using UDP Socket
Java does not have a native SNMP API, but we can simulate a basic UDP request/response mechanism
similar to SNMP polling.
SNMP-like Server (UDP)
java
CopyEdit
import [Link].*;
public class SNMPServer {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(5000)) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, [Link]);
[Link]("SNMP-like Server running...");
[Link](packet);
String request = new String([Link](), 0, [Link]());
[Link]("Received: " + request);
String response = "System Uptime: 10234 seconds";
byte[] responseBytes = [Link]();
DatagramPacket responsePacket = new DatagramPacket(
responseBytes, [Link],
[Link](), [Link]());
[Link](responsePacket);
} catch (Exception e) {
[Link]();
}
}
}
SNMP-like Client (UDP)
java
CopyEdit
import [Link].*;
public class SNMPClient {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
String message = "GET sysUptime";
byte[] buffer = [Link]();
InetAddress address = [Link]("localhost");
DatagramPacket packet = new DatagramPacket(buffer, [Link], address, 5000);
[Link](packet);
byte[] response = new byte[1024];
DatagramPacket responsePacket = new DatagramPacket(response, [Link]);
[Link](responsePacket);
String reply = new String([Link](), 0, [Link]());
[Link]("Response: " + reply);
} catch (Exception e) {
[Link]();
}
}
}
(c) File Transfer using TCP in Java
[Link] (TCP)
java
CopyEdit
import [Link].*;
import [Link].*;
public class FileServer {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(6000)) {
[Link]("File server waiting for connection...");
Socket socket = [Link]();
FileInputStream fis = new FileInputStream("[Link]");
BufferedOutputStream bos = new BufferedOutputStream([Link]());
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = [Link](buffer)) != -1) {
[Link](buffer, 0, bytesRead);
}
[Link]();
[Link]();
[Link]();
[Link]("File sent.");
} catch (IOException e) {
[Link]();
}
}
}
[Link] (TCP)
import [Link].*;
import [Link].*;
public class FileClient {
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 6000)) {
InputStream in = [Link]();
FileOutputStream fos = new FileOutputStream("[Link]");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = [Link](buffer)) != -1) {
[Link](buffer, 0, bytesRead);
}
[Link]();
[Link]("File received and saved as [Link]");
} catch (IOException e) {
[Link]();
}
}
}
Program 8
Java Program to Run Network Commands
import [Link];
import [Link];
public class NetworkCommands {
public static void runCommand(String command) {
try {
ProcessBuilder pb = new ProcessBuilder([Link](" "));
[Link](true);
Process process = [Link]();
BufferedReader reader = new BufferedReader(
new InputStreamReader([Link]()));
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
} catch (Exception e) {
[Link]("Error running command: " + [Link]());
}
}
public static void main(String[] args) {
[Link]("=== PING ===");
runCommand("ping -c 4 [Link]"); // Use `-n` on Windows
[Link]("\n=== TRACEROUTE ===");
runCommand("traceroute [Link]"); // Use `tracert` on Windows
[Link]("\n=== NSLOOKUP ===");
runCommand("nslookup [Link]");
[Link]("\n=== ARP TABLE ===");
runCommand("arp -a");
// Telnet and FTP are interactive; we show basic usage
[Link]("\n=== TELNET (will not work unless service is available) ===");
runCommand("telnet [Link]");
[Link]("\n=== FTP (may fail if no service or credentials) ===");
runCommand("ftp");
// You can add commands like netstat, ipconfig/ifconfig similarly
}
}
Program 9
Java Code: Link State Routing (Dijkstra)
import [Link].*;
class Graph {
private int vertices;
private int[][] adjacencyMatrix;
public Graph(int v) {
[Link] = v;
adjacencyMatrix = new int[v][v];
}
public void addEdge(int src, int dest, int weight) {
adjacencyMatrix[src][dest] = weight;
adjacencyMatrix[dest][src] = weight; // undirected graph
}
public void dijkstra(int source) {
int[] dist = new int[vertices];
boolean[] visited = new boolean[vertices];
int[] parent = new int[vertices];
[Link](dist, Integer.MAX_VALUE);
dist[source] = 0;
parent[source] = -1;
for (int i = 0; i < vertices - 1; i++) {
int u = minDistance(dist, visited);
visited[u] = true;
for (int v = 0; v < vertices; v++) {
if (!visited[v] && adjacencyMatrix[u][v] != 0 &&
dist[u] + adjacencyMatrix[u][v] < dist[v]) {
parent[v] = u;
dist[v] = dist[u] + adjacencyMatrix[u][v];
}
}
}
printPaths(source, dist, parent);
}
private int minDistance(int[] dist, boolean[] visited) {
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < vertices; v++) {
if (!visited[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}
private void printPaths(int source, int[] dist, int[] parent) {
[Link]("Router\t Distance from Source\tPath");
for (int i = 0; i < vertices; i++) {
if (i != source) {
[Link](source + " -> " + i + "\t\t" + dist[i] + "\t\t");
printPath(i, parent);
[Link]();
}
}
}
private void printPath(int current, int[] parent) {
if (parent[current] == -1) {
[Link](current);
return;
}
printPath(parent[current], parent);
[Link](" -> " + current);
}
}
Main Program to Run
java
CopyEdit
public class LinkStateRoutingDemo {
public static void main(String[] args) {
Graph g = new Graph(6);
[Link](0, 1, 4);
[Link](0, 2, 2);
[Link](1, 2, 1);
[Link](1, 3, 5);
[Link](2, 3, 8);
[Link](2, 4, 10);
[Link](3, 4, 2);
[Link](3, 5, 6);
[Link](4, 5, 3);
[Link](0); // Starting router (source node)
}
}
Output (Example)
rust
CopyEdit
Router Distance from Source Path
0 -> 1 3 0 -> 2 -> 1
0 -> 2 2 0 -> 2
0 -> 3 8 0 -> 2 -> 1 -> 3
0 -> 4 10 0 -> 2 -> 1 -> 3 -> 4
0 -> 5 13 0 -> 2 -> 1 -> 3 -> 4 -> 5
Program 10
Java Implementation of Distance Vector Routing
Sample Network Topology
Router Connected to (Cost)
Router Connected to (Cost)
A B(2), C(5)
B A(2), C(1), D(3)
C A(5), B(1), D(2)
D B(3), C(2)
Java Code
import [Link].*;
class DistanceVectorRouting {
static final int INF = 9999;
int[][] cost; // adjacency matrix
int[][] distance; // distance vectors
int[][] nextHop;
int routers;
public DistanceVectorRouting(int r) {
routers = r;
cost = new int[routers][routers];
distance = new int[routers][routers];
nextHop = new int[routers][routers];
for (int i = 0; i < routers; i++) {
for (int j = 0; j < routers; j++) {
if (i == j)
cost[i][j] = 0;
else
cost[i][j] = INF;
distance[i][j] = cost[i][j];
nextHop[i][j] = j;
}
}
}
public void addEdge(int src, int dest, int c) {
cost[src][dest] = c;
cost[dest][src] = c;
distance[src][dest] = c;
distance[dest][src] = c;
}
public void calculateDistanceVectors() {
boolean updated;
do {
updated = false;
for (int i = 0; i < routers; i++) {
for (int j = 0; j < routers; j++) {
for (int k = 0; k < routers; k++) {
if (distance[i][j] > cost[i][k] + distance[k][j]) {
distance[i][j] = cost[i][k] + distance[k][j];
nextHop[i][j] = k;
updated = true;
}
}
}
}
} while (updated);
}
public void printRoutingTable() {
for (int i = 0; i < routers; i++) {
[Link]("Routing table for router " + (char)('A' + i) + ":");
[Link]("Destination\tCost\tNext Hop");
for (int j = 0; j < routers; j++) {
if (i != j) {
[Link]((char)('A' + j) + "\t\t" + distance[i][j] + "\t" + (char)('A' + nextHop[i][j]));
}
}
[Link]();
}
}
}
▶ Main Program
java
CopyEdit
public class DVRoutingDemo {
public static void main(String[] args) {
DistanceVectorRouting dvr = new DistanceVectorRouting(4); // A, B, C, D
// Add edges: A=0, B=1, C=2, D=3
[Link](0, 1, 2); // A-B
[Link](0, 2, 5); // A-C
[Link](1, 2, 1); // B-C
[Link](1, 3, 3); // B-D
[Link](2, 3, 2); // C-D
[Link]();
[Link]();
}
}
🧾 Sample Output
less
CopyEdit
Routing table for router A:
Destination Cost Next Hop
B 2 B
C 3 B
D 6 B
Routing table for router B:
Destination Cost Next Hop
A 2 A
C 1 C
D 3 D
...
Program 11
Implementation of Subnetting
Network Address
Broadcast Address
First and Last Host Address
Number of Hosts
Java Code: Subnetting Implementation
java
CopyEdit
import [Link];
public class SubnetCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter IP address (e.g., [Link]): ");
String ipAddress = [Link]();
[Link]("Enter CIDR notation (e.g., 24): ");
int cidr = [Link]();
try {
String[] parts = [Link]("\\.");
int ip = 0;
for (String part : parts) {
ip = (ip << 8) | [Link](part);
}
int mask = 0xFFFFFFFF << (32 - cidr);
int network = ip & mask;
int broadcast = network | ~mask;
int firstHost = network + 1;
int lastHost = broadcast - 1;
int numberOfHosts = (int) [Link](2, (32 - cidr)) - 2;
[Link]("\n--- Subnetting Details ---");
[Link]("Network Address: " + intToIp(network));
[Link]("Broadcast Address: " + intToIp(broadcast));
[Link]("First Host Address: " + intToIp(firstHost));
[Link]("Last Host Address: " + intToIp(lastHost));
[Link]("Number of Hosts: " + numberOfHosts);
} catch (Exception e) {
[Link]("Invalid IP address or CIDR input.");
}
}
private static String intToIp(int ip) {
return ((ip >> 24) & 0xFF) + "." +
((ip >> 16) & 0xFF) + "." +
((ip >> 8) & 0xFF) + "." +
(ip & 0xFF);
}
}
Sample Input:
Enter IP address (e.g., [Link]): [Link]
Enter CIDR notation (e.g., 24): 24
Sample Output:
--- Subnetting Details ---
Network Address: [Link]
Broadcast Address: [Link]
First Host Address: [Link]
Last Host Address: [Link]
Number of Hosts: 254