Simple Wireless Program in NS2 is the best way to learn about how to code in NS2. NS2 is one of the best simulation tool used by majority of scholars today due to its highlighted features like support 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. Regarding wired simulation in NS2, refer our other articles available in this site. Here, we have taken a simple wireless program in NS2 to explain the students about how to work with wireless networks in NS2. For further guidance and tutoring service on NS2, approach us anytime, we are there for you at 24/7.


       Let’s take a simple example to explain you regarding wireless simulation in NS2. Here, we have considered DSDV Ad hoc Routing protocol with 50 wireless nodes. Now, let’s see the overall step used to create a simple wireless program in NS2.

First, we have to create a TCL script [For ex. Filename.tcl].

Define the following options in TCL script.


set val(chan)   Channel/WirelessChannel

set val(prop)   Propagation/TwoRayGround

set val(ant)    Antenna/OmniAntenna

set val(ll)     LL

set val(ifq)    Queue/DropTail/PriQueue

set val(ifqlen) 50

set val(netif)  Phy/WirelessPhy

set val(mac)    Mac/802_11

set val(rp)     DSDV

set val(nn)     50

set val(x)      2000

set val(y)      1000

set val(stop)   10

set val(traffic)        cbr

set val(traffic)        tcp

Next, we need to create an instance of Simulator using

set ns    [new Simulator]

             Then creating the trace file and nam file

set     tracefd       [open w]

$ns    trace-all   $tracefd

set   namtrace       [open out.nam w]

$ns   namtrace-all-wireless   $namtrace  $val(x)  $val(y)

Set up topology object and create object god

set topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

                           set god_ [create-god $val(nn)]//to create god object

 Create Nodes:

First, we need to configure nodes, before creating it.

                  //To 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


//To create Wireless nodes[50 nodes]:

//Creating node objects

for {set i 0} {$i < $val(nn) } { incr i } {

set node_($i) [$ns node]


//To define node initial position in nam

for {set i 0} {$i < $val(nn)} { incr i } {

# 30 defines the node size for nam

$ns initial_node_pos $node_($i) 30


//use of random mobility positions

proc destination {} {

global ns val node_

set time 1.0

set now [$ns now]

for {set i 0} {$i<$val(nn)} {incr i} {

set xx [expr rand()*1600]

set yy [expr rand()*800]

$ns at $now “$node_($i) setdest $xx $yy 1000.0”


$ns at [expr $now+$time] “destination”


//To change node colors at the time of 1.0 , 2.0 , 3.0

for {set i 0} {$i < $val(nn) } {incr i } {

$node_($i) color yellow

$ns at 1.0 “$node_($i) color red”


for {set i 0} {$i < $val(nn) } {incr i } {

$node_($i) color yellow

$ns at 2.0 “$node_($i) color lightgreen”


for {set i 0} {$i < $val(nn) } {incr i } {

$node_($i) color yellow

$ns at 3.0 “$node_($i) color orange”


//To end nam and the simulation

$ns at $val(stop) “$ns nam-end-wireless $val(stop)”

$ns at $val(stop) “stop”

$ns at 10.5 “puts \”end simulation\” ; $ns halt”

// Execute the nam file

proc stop {} {

global ns tracefd namtrace

$ns flush-trace

close $tracefd

close $namtrace

exec nam out.nam &


$ns run

 //To run program

$ns filename.tcl



       Hope you would get an idea about how to create a simple wireless program in NS2. For further guidance on more complex programs or on a particular topic, approach our experts, waiting to serve you at 24/7. You can feel free to approach us for any kind of help regarding NS2 based projects, assignments or lab cycles. You will be surely benefitted with our work, as we offer project/concept based learning approach. You will get complete knowledge about NS2 along with our implementation support for your project/particular concept. Approach us, if you want to experience our work and standard.



              TRY HARD TO ACHIEVE YOUR SUCCESS ………………………….

                               WITH THE HELP OF OUR GUIDANCE…………………..