How to create Wireless Network using NS2 simulator

January 23, 2018 Author: munishmishra04_3od47tgp
Print Friendly, PDF & Email

NS2 is one of the best simulation tool used by bulk of scholars today due to its highlighted features like sustain for OOPs concept, C++ programming fundamentals, real time emulation support etc. NS2 is used to simulate both wired and wireless networks; here we have focused on wireless network simulation in NS2 due to its wide applicability.

We are going to simulate a very simple 2-node wireless scenario. The topology consists of two mobile nodes,  and  the mobile nodes move about within an area whose boundary is defined in this example as. The nodes start firstly at two opposite ends of the boundary. Then they move towards each other in the first half of the simulation and again move away for the second half. A TCP connection is setup between the two mobile nodes. Packets are exchanged between the nodes as they come within hearing range of one another.

The simulation script is representing here for 2 wireless nodes in network simulation version-2.

Simulation Script

#Filename: MyFirstSimulation.tcl

#TCL – Tool Command Language

# Simulator Instance Creation
set ns [new Simulator]

#Fixing the co-ordinate of simulation area
set val(x) 500
set val(y) 500
# Define options
set val(chan) Channel/WirelessChannel // channel type
set val(prop) Propagation/TwoRayGround // radio-propagation model

set val(netif) Phy/WirelessPhy // network interface type
set val(mac) Mac/802_11 // MAC type
set val(ifq) Queue/DropTail/PriQueue // interface queue type
set val(ll) LL // link layer type
set val(ant) Antenna/OmniAntenna // antenna model
set val(ifqlen) 50 // max packet in ifq
set val(nn) 2 // number of mobilenodes
set val(rp) AODV // routing protocol
set val(x) 500 // X dimension of topography
set val(y) 400 // Y dimension of topography
set val(stop) 10.0 // time of simulation end

# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

#Nam File Creation nam – network animator
set namfile [open MyFirstSimulation.nam w]

#Tracing all the events and cofiguration
$ns namtrace-all-wireless $namfile $val(x) $val(y)

#Trace File creation
set tracefile [open w]

#Tracing all the events and configuration
$ns trace-all $tracefile

# General operational descriptor- storing the hop details in the network
create-god $val(nn)

# configure the nodes
$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 OFF \
-movementTrace ON

# Node Creation
set node1 [$ns node]
# Initial color of the node
$node1 color black

#Location fixing for a single node
$node1 set X_ 200
$node1 set Y_ 100
$node1 set Z_ 0

set node2 [$ns node]
$node2 color black

$node2 set X_ 200
$node2 set Y_ 300
$node2 set Z_ 0
# Label and coloring
$ns at 0.1 “$node1 color blue”
$ns at 0.1 “$node1 label Node1”
$ns at 0.1 “$node2 label Node2”
#Size of the node
$ns initial_node_pos $node1 40
$ns initial_node_pos $node2 40
# ending nam and the simulation
$ns at $val(stop) “$ns nam-end-wireless $val(stop)”
$ns at $val(stop) “stop”

#Stopping the scheduler
$ns at 10.01 “puts \”end simulation\”; $ns halt”
#$ns at 10.01 “$ns halt”
proc stop {} {
global namfile tracefile ns
$ns flush-trace
close $namfile
close $tracefile
#executing nam file
exec nam MyFirstSimulation.nam &

#starting scheduler
$ns run

ns MyFirstSimulation.tcl

wireless simulation using NS2


[1] The Network Simulator – ns-2, available online at:

[2] “Basic Example in NS2 (Two node and link)”, available online at:

[3] “NS2 Program Tutorial for Wireless Topology”, available online at:

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Additional settings
Formula color
Text color
Type math using LaTeX
Nothing to preview