How to Implement Ad Hoc Networks in ns2

To implement the Ad Hoc Network in ns2 (Network Simulator 2), we have to simulate a network that has nodes which interacts with one another without depending on any fixed facilities like routers or access points. In this network, every node behaves like both host and a router, dynamically finding and upholding paths to other nodes within the network. Follow the given guide to implement this in ns2:

Step-by-Step Implementation:

Conceptual Overview

In an Ad Hoc Network:

  1. Nodes: These are the devices (such as laptops, mobile devices) that interact with one another directly.
  2. Routing Protocols: Since the network is decentralized, routing protocols like AODV (Ad hoc On-Demand Distance Vector), DSR (Dynamic Source Routing), or DSDV (Destination-Sequenced Distance-Vector) are used to handle the dynamic routing of data.
  3. Mobility Models: Nodes in an Ad Hoc Network are frequent mobile, and their movement can impact connectivity and route constancy.

Step 1: Conceptualize the Ad Hoc Network Simulation

In this simulation, we will generate a situation in which the several nodes communicate with one another using ad hoc routing protocol like AODV. The nodes will be mobile, following a random waypoint mobility model.

Step 2: Create the Tcl Script

Use the AODV routing protocol to simulate a simple scenario. You have to follow the given example Tcl script

Example Tcl Script for Simulating an Ad Hoc Network in ns2

# Create a simulator object

set ns [new Simulator]

# Define the topography object

set topo [new Topography]

$topo load_flatgrid 1000 1000  # 1km x 1km area

# Create the General Operations Director (GOD) for wireless simulations

create-god 10  # Number of nodes

# Configure the nodes for the Ad Hoc Network using AODV

$ns node-config -adhocRouting AODV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail/PriQueue \

-ifqLen 50 \

-antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy \

-channelType Channel/WirelessChannel \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace ON \

-movementTrace ON

# Open trace and NAM files for recording the simulation

set tracefile [open ad_hoc_out.tr w]

$ns trace-all $tracefile

set namfile [open ad_hoc_out.nam w]

$ns namtrace-all-wireless $namfile 1000 1000

# Define a finish procedure to close files and end the simulation

prc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam ad_hoc_out.nam &

exit 0

}

# Create nodes for the Ad Hoc Network

for {set i 0} {$i < 10} {incr i} {

set node($i) [$ns node]

}

# Set initial positions for nodes (randomly placed)

$node(0) set X_ 100.0

$node(0) set Y_ 200.0

$node(1) set X_ 300.0

$node(1) set Y_ 400.0

$node(2) set X_ 500.0

$node(2) set Y_ 600.0

$node(3) set X_ 700.0

$node(3) set Y_ 800.0

$node(4) set X_ 900.0

$node(4) set Y_ 200.0

$node(5) set X_ 200.0

$node(5) set Y_ 500.0

$node(6) set X_ 400.0

$node(6) set Y_ 700.0

$node(7) set X_ 600.0

$node(7) set Y_ 900.0

$node(8) set X_ 800.0

$node(8) set Y_ 100.0

$node(9) set X_ 1000.0

$node(9) set Y_ 300.0

# Define mobility for the nodes using the Random Waypoint Model

for {set i 0} {$i < 10} {incr i} {

$ns at [expr 1.0 * $i] “$node($i) setdest [expr 1000.0 * rand()] [expr 1000.0 * rand()] [expr 10.0 * rand()]”

}

# Define a custom procedure for simulating data transmission between nodes

proc send_data {src dst packetSize rate} {

global ns

# Create a UDP agent to simulate data traffic

set udp [new Agent/UDP]

$ns attach-agent $src $udp

set null [new Agent/Null]

$ns attach-agent $dst $null

$ns connect $udp $null

# Generate data traffic using a CBR application

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set packetSize_ $packetSize

$cbr set rate_ $rate

$cbr start

}

# Simulate data transmission between nodes

$ns at 10.0 “send_data $node(0) $node(9) 512 100Kb”

$ns at 20.0 “send_data $node(1) $node(8) 512 100Kb”

$ns at 30.0 “send_data $node(2) $node(7) 512 100Kb”

# Schedule the end of the simulation

$ns at 60.0 “finish”

# Run the simulation

$ns run

Step 3: Run the Tcl Script

Save the script with a .tcl extension, For instance, ad_hoc_simulation.tcl. Then, execute the script in your terminal by following the given command:

ns ad_hoc_simulation.tcl

Step 4: Visualize the Simulation

To visualize the simulation, open the produced NAM file using:

nam ad_hoc_out.nam

Script Explanation

  • Node Configuration: The nodes are sets up to utilize the AODV routing protocol, which is perfect for ad hoc networks where routes are accomplished on requirements. Mobility: The setdest commands mimic node mobility using a Random Waypoint Mobility Model, where nodes travel to random endpoints at random speeds.
  • Data Transmission: The send_data procedure simulates data transfers amongst nodes using UDP, which is usual in numerous ad hoc network situations because of its simplicity and low overhead.

Customization

  • Different Routing Protocols: Research with other routing protocols like DSR or DSDV to observe how they accomplish in several ad hoc network scenarios.
  • Varying Mobility Models: Simulate different kinds of node movement patterns by implementing various mobility models (such as Manhattan Grid, Gauss-Markov).
  • Traffic Patterns: Test with various traffic patterns (like TCP instead of UDP, changing data rates) to simulate several types of applications.
  • Scalability: Maximize the count of the nodes and the area size to replicate larger and more difficult ad hoc networks.

Limitations

  • Simplified Mobility: The Random Waypoint model used in this instance is simple and may not precisely reflect real-world mobility patterns.
  • No Physical Layer Simulation: It doesn’t simulate the physical layer characteristics particular to an ad hoc networks include signal fading, intrusion, or multipath effects.
  • Limited Protocol Support: ns2 has narrow support for modern ad hoc network protocols and strategies, which may restrict the realism of the simulation.

This approach will help you to understand, how to set up ns2 and the implementation of the ad hoc networks in the ns3 tool with their limitations and customization process. If needed, we can clarify you about the concerns you have regarding this topic.

Just provide your research details, and we will share with you innovative Ad Hoc Network projects along with their implementation results. At ns2project.com, you will receive the best research outcomes accompanied by comprehensive results.