To simulate on-demand routing protocols in ns2 has needs to follow numerous steps to follow and it includes configuring a wireless ad hoc network, prioritizing the on-demand routing protocol, describing traffic flows, and measuring the outcomes. The on-demand routing protocols such as Ad hoc On-Demand Distance Vector (AODV), Dynamic Source Routing (DSR).
Below are the procedures to simulate the on-demand routing protocols in ns2
Steps to Simulate On-Demand Routing Protocols in NS2
- Install NS2
Make sure NS2 is installed on the system. We can install it on Ubuntu with the following command:
sudo apt-get install ns2
Validates that the installation is completed, type ns in the terminal. If it’s properly installed we will see the NS2 prompt (%).
- Choose On-Demand Protocols in NS2
NS2 supports a variety of on-demand routing protocols like:
- AODV (Ad-hoc On-demand Distance Vector)
We can set up these protocols in Tcl script by requiring the routing protocol in the simulation setup.
- Create a Tcl Simulation Script
Here is an instance Tcl script to replicate an ad hoc network using AODV by way of the routing protocol.
# Create a new simulator object
set ns [new Simulator]
# Define the trace file and the nam output file
set tracefile [open out.tr w]
set namfile [open out.nam w]
$ns trace-all $tracefile
$ns namtrace-all-wireless $namfile 500 500
# Define the topology
set topo [new Topography]
$topo load_flatgrid 500 500
# Configure the nodes for a wireless environment
set opt(chan) Channel/WirelessChannel
set opt(prop) Propagation/TwoRayGround
set opt(netif) Phy/WirelessPhy
set opt(mac) Mac/802_11
set opt(ifq) Queue/DropTail/PriQueue
set opt(ll) LL
set opt(ant) Antenna/OmniAntenna
set opt(ifqlen) 50
set opt(nn) 5 ;# Number of nodes
set opt(rp) AODV ;# Routing protocol (AODV, DSR)
# Configure the wireless nodes
create-god $opt(nn)
for {set i 0} {$i < $opt(nn)} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0 ;# Disable random motion
}
# Set node initial positions
$node_(0) set X_ 100
$node_(0) set Y_ 200
$node_(0) set Z_ 0
$node_(1) set X_ 200
$node_(1) set Y_ 300
$node_(1) set Z_ 0
$node_(2) set X_ 300
$node_(2) set Y_ 400
$node_(2) set Z_ 0
$node_(3) set X_ 400
$node_(3) set Y_ 500
$node_(3) set Z_ 0
$node_(4) set X_ 100
$node_(4) set Y_ 500
$node_(4) set Z_ 0
# Define traffic pattern (CBR – Constant Bit Rate)
set udp [new Agent/UDP]
$ns attach-agent $node_(0) $udp
set null [new Agent/Null]
$ns attach-agent $node_(4) $null
$ns connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ 512
$cbr set rate_ 0.5Mb
# Schedule the traffic
$ns at 1.0 “$cbr start”
# Finish the simulation at 10.0 seconds
$ns at 10.0 “finish”
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
# Run the simulation
$ns run
- Run the Tcl Script
Save the Tcl script as on_demand.tcl. we can execute the script with:
ns on_demand.tcl
This will generate two output files:
- out.tr — The trace file containing detailed simulation information.
- out.nam — The Network Animator file used for visualizing the network.
- Visualize the Simulation with NAM
We can open the NAM file to envision the packet transmissions and node movements:
nam out.nam
NAM will open a graphical interface that enables you to see the movement of nodes and how data flows among them in real-time.
- Analyse the Trace File
The trace file (out.tr) encompasses information about the simulation, like packet transmissions, receptions, drops, routing decisions, and more. We can utilize AWK, Perl, or Python scripts to parse and evaluate the trace file for parameters such as throughput, delay, packet delivery ratio, etc.
For instance, to estimate the number of packets received by the destination, we need to utilize AWK:
awk ‘($1 == “r” && $4 == “tcp”) {recv++} END {print “Packets Received: ” recv}’ out.tr
- Simulating Different On-Demand Protocols
We can change the protocol from AODV to other on-demand protocols by simply adjusting the routing protocol parameter in the script.
For DSR (Dynamic Source Routing), adjust the line:
set opt(rp) DSR
The rest of the Tcl script remains the same. We can rerun the simulation and evaluate the performance of DSR in comparison to AODV.
- Advanced Scenario Modifications
To replicate more complex scenarios, we can:
- Increase the number of nodes: Adjust the value of opt(nn) to describe a larger network.
- Vary the mobility model: Establish random node movements by permit random motion.
- Define different traffic types: Utilize FTP, Telnet, or CBR traffic patterns with diverse rates and packet sizes.
- Change the network topology: Adapt node positions and link parameters (bandwidth, delay).
- Performance Evaluation
Once the simulation is done, we can extract parameters to measure the routing protocols:
- Throughput: Evaluate the total data delivered to the destination.
- End-to-End Delay: Estimate the average time taken for a packet to travel from the source to the destination.
- Packet Delivery Ratio (PDR): Compute the ratio of packets received to packets sent.
We can utilize the tools such as AWK or MATLAB to measure the trace file and generate graphs relates the performance of diverse protocols in numerous conditions.
- Troubleshooting
- Simulation crashes: validate for syntax errors in the Tcl script. NS2 will usualyy show the error line in the terminal.
- No output generated: Make sure $ns run is called at the termination of the script and which the NAM and trace files are appropriately set.
We offered the basic to advanced essential information that helps you understand the core concepts on how to simulate and evaluate the on-demand routing protocols projects using the tool of ns2. More information regarding this process will be offered later.
Please provide us with the details of your On-Demand Protocol Projects. We will ensure you receive excellent simulation guidance along with valuable research ideas and project topics. If you seek top-notch assistance on on-demand routing protocols like Ad hoc On-Demand Distance Vector (AODV) and Dynamic Source Routing (DSR), phdprime.com is here to offer you comprehensive support.