0% found this document useful (0 votes)
3 views

Assignment 1

asss

Uploaded by

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

Assignment 1

asss

Uploaded by

Anurag Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignmant-1.

WAP to create a Router in java that involves simulating routing


functions, such as handling incoming packets, maintaining a routing table, and
forwarding packets to the correct destination.
CODE: This implementation focuses on a console-based simulation.
Explanation

1. Packet Class: Represents a network packet with a destination and a message.


2. Router Class:
o Maintains a routing table (a map of destinations to next hops).
o Can add routes and route packets based on the routing table.
3. Routing Logic: When a packet is routed, it checks the routing table for the destination. If
a route exists, it simulates sending the packet.
4. SimpleRouterApp Class: The main application that allows users to input destination IPs
and messages to route.

this.destination = destination;

this.message = message;

} import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

class Packet

String destination;

String message;

public Packet(String destination, String message)

class Router
{

private Map<String, String> routingTable;


public Router()
{
routingTable = new HashMap<>();

public void addRoute(String destination, String nextHop)


{

routingTable.put(destination, nextHop);

public void routePacket(Packet packet)


{

String nextHop = routingTable.get(packet.destination);

if (nextHop != null)
{

System.out.println("Routing packet to " + packet.destination + " via " + nextHop);

// Simulate sending the packet

sendPacket(packet, nextHop);

}
else

System.out.println("No route found for destination: " + packet.destination);

private void sendPacket(Packet packet, String nextHop)


{

// Simulate packet delivery

System.out.println("Packet sent to " + nextHop + ": " + packet.message);

}
}

public class SimpleRouterApp


{

public static void main(String[] args)


{

Router router = new Router();

// Add some routes to the routing table

router.addRoute("192.168.1.1", "Router1");

router.addRoute("192.168.1.2", "Router2");

router.addRoute("192.168.1.3", "Router3");

Scanner scanner = new Scanner(System.in);

while (true)
{

System.out.print("Enter destination IP (or 'exit' to quit): ");

String destination = scanner.nextLine();

if (destination.equalsIgnoreCase("exit"))

break;

System.out.print("Enter message: ");

String message = scanner.nextLine();

Packet packet = new Packet(destination, message);

router.routePacket(packet);

scanner.close();

You might also like