To simulate Shortest Path Routing (SPR) projects in NS2, we will require setting up the network to enthusiastically estimate and utilize the shortest path among nodes. In NS2, the shortest path routing is usually a part of the default behaviour in many routing protocols, like Link State Routing (LSR) or Distance Vector Routing (DVR), however we can explicitly concentrate on configuring the environment for a shortest path-based network.
Steps to Simulate Shortest Path Routing in NS2
- Install NS2
Ensure that ns2 is installed on the system.
- Define a Network Topology
In NS2, we want to generate a TCL (Tool Command Language) script that describes the network, nodes, and connections. In this script, we will set up routing to identify the shortest path among the nodes.
- Use a Routing Protocol for Shortest Path
Most routing protocols estimate the shortest path by default. For this replication, we can weather to utilize Link State Routing (LSR) that clearly estimates the shortest path using the Dijkstra algorithm, or Distance Vector Routing.
Link State Routing (Dijkstra’s Algorithm) is generally used for shortest path routing.
Example TCL Script for Shortest Path Routing:
# Define the simulator
set ns [new Simulator]
# Define trace and NAM files for visualization
set tracefile [open “spr_trace.tr” w]
set namfile [open “spr_simulation.nam” w]
$ns trace-all $tracefile
$ns namtrace-all $namfile
# Create network nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
# Create duplex links between the nodes (bandwidth and delay)
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 20ms DropTail
$ns duplex-link $n1 $n2 1Mb 15ms DropTail
$ns duplex-link $n1 $n3 1Mb 10ms DropTail
$ns duplex-link $n2 $n4 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 5ms DropTail
# Configure shortest path routing (by default, NS2 uses shortest path)
for {set i 0} {$i < 5} {incr i} {
$ns at 0.0 “$n$i start-routing”
}
# Define a UDP agent and attach it to node 0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Define a CBR traffic generator and attach it to UDP agent
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 512
$cbr0 set rate_ 0.5Mb
$cbr0 attach-agent $udp0
# Attach a UDP sink to destination node (n4)
set udp1 [new Agent/UDP]
$ns attach-agent $n4 $udp1
# Connect UDP agents
$ns connect $udp0 $udp1
# Set traffic flow
$ns at 1.0 “$cbr0 start”
$ns at 4.0 “$cbr0 stop”
# Define the simulation end time and finish procedure
$ns at 5.0 “finish”
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exit 0
}
# Start the simulation
$ns run
- Run the Simulation
Save the script as shortest_path_routing.tcl, and execute it using NS2 with the following command:
ns shortest_path_routing.tcl
- Visualize the Simulation
Open the Network Animator (NAM) to envision on how the packets are transmitted through the shortest path:
nam spr_simulation.nam
- Analyse the Trace File
The spr_trace.tr file will have the thorough details of the simulation that has the packet forwarding, transmission time, and other parameters. We can parse this file to evaluate:
- End-to-end delay
- Throughput
- Packet delivery ratio
- Route taken (to verify if it’s the shortest path)
- Modifying the Topology
We can modify the latency or bandwidth of links among the nodes and track on how the routing protocol modifies to always select the shortest path based on delay or cost.
Optional Enhancements:
- Traffic Analysis: Establish TCP traffic sources and replicate larger networks with changing traffic loads.
- Comparative Analysis: Mimic alternative routing protocols like DVR and relate their performance with shortest path routing.
- Failures in Nodes/Links: Establish failures in nodes or links to validate on how the shortest path changes dynamically.
By using the ns2 tool, we illustrate the basic to advanced approach with sample coding for Shortest Path Routing that were simulated, enhanced and visualized the outcomes. Further details will be provided later.
You can find awesome project ideas in this field from the experts at phdprime.com. All you need to do is share your information with us, and we’ll help you improve your project on Shortest Path Routing with the NS2 tool. Our easy-to-follow instructions will show you how to successfully simulate Shortest Path Routing in your projects using NS2.