To simulate wireless projects in Network Simulator 2 (NS2) has includes to set up wireless nodes, mobility models, wireless communication protocols, and network traffic patterns to implement real-world wireless network environment. NS2 delivered built-in support for wireless networks, that contain the behaviours for mobile ad-hoc networks (MANETs), wireless sensor networks (WSNs), and more.
Here’s a step-by-step guide to simulate wireless network projects using NS2.
Steps for Simulating Wireless Projects in NS2
- Install NS2: If we haven’t already installed NS2, we can install it on Linux (Ubuntu, Fedora, etc.) by processing the following command:
sudo apt-get install ns2
Verify that NS2 is installed by running:
ns -version
- Understand NS2 Wireless Components: NS2 deliver to support for wireless simulations using the following key elements:
- MobileNode: Represents a wireless node, contain the MAC, link layer, network layer, and application layer.
- WirelessChannel: Design the shared wireless medium for communication among nodes.
- Mobility Models: Describes how the nodes move in the simulation area (e.g., Random Waypoint, Random Walk).
- Traffic Models: signifies different kinds of network traffic like Constant Bit Rate (CBR) over UDP, or FTP over TCP.
- Basic Wireless Network Simulation in NS2 Using Tcl Script: Here’s a core sample of a Tcl script to replicate a simple wireless network with two mobile nodes:
Example Tcl Script for a Simple Wireless Network Simulation
# Create a new NS2 simulator object
set ns [new Simulator]
# Open trace and NAM files for logging
set tracefile [open wireless_out.tr w]
$ns trace-all $tracefile
set namfile [open wireless_out.nam w]
$ns namtrace-all-wireless $namfile 500 500
# Define the wireless channel for communication
set val(chan) Channel/WirelessChannel ;# Channel type
set val(prop) Propagation/TwoRayGround ;# Propagation model
set val(netif) Phy/WirelessPhy ;# Network interface type
set val(mac) Mac/802_11 ;# MAC type (Wi-Fi)
set val(ifq) Queue/DropTail/PriQueue ;# Interface Queue
set val(ll) LL ;# Link Layer
set val(ant) Antenna/OmniAntenna ;# Antenna type
set val(ifqlen) 50 ;# Max packet in IFQ
set val(x) 500 ;# X dimension of the topology
set val(y) 500 ;# Y dimension of the topology
set val(nn) 2 ;# Number of mobile nodes
set val(rp) DSDV ;# Routing protocol
# Create the network topology object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# Configure the simulator with the channel and mobility settings
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON
# Create mobile nodes
for {set i 0} {$i < $val(nn)} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0 ;# Disable random motion initially
}
# Set node positions
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 150.0
$node_(1) set Y_ 150.0
$node_(1) set Z_ 0.0
# Define traffic flow: UDP over CBR
set udp [new Agent/UDP]
$ns attach-agent $node_(0) $udp
set null [new Agent/Null]
$ns attach-agent $node_(1) $null
$ns connect $udp $null
# Create a CBR application to simulate traffic
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 512
$cbr set interval_ 0.01 ;# 100 packets per second
$cbr attach-agent $udp
# Schedule traffic start and stop times
$ns at 1.0 “$cbr start”
$ns at 4.5 “$cbr stop”
# Mobility for node 0 (Random Waypoint)
$ns at 0.5 “$node_(0) setdest 400.0 400.0 10.0”
# Simulation end
$ns at 6.0 “finish”
# Define finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam wireless_out.nam &
exit 0
}
# Run the simulation
$ns run
Explanation of the Tcl Script:
- Simulator Object:
- The line set ns [new Simulator] initializes the NS2 simulation engine.
- Trace and NAM Files:
- The trace file (wireless_out.tr) stores detailed simulation data for parameters, since the NAM file (wireless_out.nam) is utilized for envision the simulation in Network Animator (NAM).
- Wireless Network Configuration:
- The wireless channel is configured using the Channel/WirelessChannel model, with a TwoRayGround propagation model and 802.11 MAC layer (Wi-Fi).
- The OmniAntenna delivers an omnidirectional signal for the wireless nodes.
- The routing protocol is set to DSDV (Destination-Sequenced Distance-Vector), however we can also utilize protocols such as AODV, DSR, or TORA.
- Node Creation and Mobility:
- Two mobile nodes (node_(0) and node_(1)) are generated, with initial positions set using the set X_, set Y_, and set Z_ commands.
- Mobility is emulated for node 0 using the setdest command to move it to a new destination (400, 400) at a speed of 10 units per second.
- Traffic Model (UDP over CBR):
- A UDP agent is attached to node_(0), and a Null agent is attached to node_(1) to perform as a receiver.
- CBR (Constant Bit Rate) traffic is created with 512-byte packets sent at a rate of 100 packets per second (10ms intervals).
- Simulation Events:
- The CBR traffic initiates at 1.0 seconds and terminate at 4.5 seconds. The simulation ends at 6.0 seconds, at which point the finish procedure is called to close files and open NAM for visualization.
- Running the Simulation:
- Save the script as wireless_simulation.tcl and execute it in NS2:
ns wireless_simulation.tcl
- After the simulation done, open the NAM visualizer to see the wireless network:
nam wireless_out.nam
- Analysing the Results:
- Utilize the trace file (wireless_out.tr) to measure parameters such as throughput, packet loss, and end-to-end delay. We need to utilize AWK scripts or other tools to process the trace file and estimate parameters.
Extending the Simulation:
- Add More Mobile Nodes:
- Expand the simulation by adding more mobile nodes and increasing the size of the topology (area) to replicate larger wireless networks.
- Use Different Routing Protocols:
- Test with diverse wireless routing protocols like an AODV (Ad-hoc On-Demand Distance Vector), DSR (Dynamic Source Routing), or TORA (Temporally Ordered Routing Algorithm) to learn their performance.
- Change Mobility Models:
- Adjust the mobility characteristics of the nodes by utilizing mobility models such as Random Waypoint, Random Walk, or Gauss-Markov. We can also establish real-world mobility traces for more realistic simulations.
- Traffic Variations:
- Incorporate different traffic kinds like FTP over TCP, VoIP over UDP, or video streaming to replicate real-world wireless traffic environment.
- Advanced Wireless Networks:
- Mimic more complex wireless networks like Wireless Sensor Networks (WSNs), Vehicular Ad-Hoc Networks (VANETs), or cellular networks by adjusting the topology, traffic patterns, and mobility models.
From the demonstration we completely aggregate the information about the installation process and simulation procedure for wireless projects that were deploy in the tool of ns2. More information regarding the wireless projects will also be provided.
Our experts specialize in mobile ad-hoc networks (MANETs) and wireless sensor networks (WSNs) related to your projects, so we share with you novel project ideas and topics. Share your Wireless Projects details with us, and we will provide tailored simulation support.