Wednesday, March 19, 2014

ns2 programs


Here you can find the NS2 Basic Programs for practice purpose:- 


#########WIRELESS NETWORK############
#NODE CREATION#
set x 500
set y 500
set n 2
set ns_ [new Simulator]
set topo [new Topography]
$topo load_flatgrid $x $y
set tracefd [open wireless.tr w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtracefd [open first.nam w]
$ns_ namtrace-all-wireless $namtracefd $x $y
set god_ [create-god $n]
$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 OFF
for {set i 0} { $i < $n} {incr i} {
set node_($i) [$ns_ node]
}
for {set i 0} {$i < $n} {incr i} {
$ns_ initial_node_pos $node_($i) 30
}
$ns_ at 100.0 "$ns_ halt"
#Run the simulation
$ns_ run

OUTPUT


WIRELESS NETWORK
NODE MOVEMENT

set x 500
set y 500
set n 2
set ns_ [new Simulator]
set topo [new Topography]
$topo load_flatgrid $x $y
set tracefd [open wireless.tr w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtracefd [open first.nam w]
$ns_ namtrace-all-wireless $namtracefd $x $y
set god_ [create-god $n]
$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 OFF
for {set i 0} { $i < $n} {incr i} {
set node_($i) [$ns_ node]
}
#MOBILE NODE POSITION
#node position -static (fixed ) topology
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 150.0
$node_(1) set Z_ 0.0
for {set i 0} {$i < $n} {incr i} {
$ns_ initial_node_pos $node_($i) 30
}
$ns_ at 100.0 "$ns_ halt"
#Run the simulation
$ns_ run

OUTPUT
 
 


MOBILE NODE TRAFFIC UDP
set x 500
set y 500
set n 2
set ns_ [new Simulator]
set topo [new Topography]
$topo load_flatgrid $x $y
set tracefd [open wireless.tr w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtracefd [open first.nam w]
$ns_ namtrace-all-wireless $namtracefd $x $y
set god_ [create-god $n]
$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 OFF
for {set i 0} { $i < $n} {incr i} {
set node_($i) [$ns_ node]
}
#UDP DATA TRAFFIC
#UDP Data Traffic
#create UDP source
set udp0 [new Agent/UDP]
$ns_ attach-agent $node_(0) $udp0
#create UDP destination
set null0 [new Agent/Null]
$ns_ attach-agent $node_(1) $null0
#connect UDP src and destination
$ns_ connect $udp0 $null0
#create Application traffic
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.1
#Application start time
$ns_ at 1 "$cbr0 start"
#Application stop time
$ns_ at 10 "$cbr0 stop"
for {set i 0} {$i < $n} {incr i} {
$ns_ initial_node_pos $node_($i) 30
}
$ns_ at 100.0 "$ns_ halt"
#Run the simulation

$ns_ run

output





MOBILE NODE TRAFFIC TCP
set x 500
set y 500
set n 2
set ns_ [new Simulator]
set topo [new Topography]
$topo load_flatgrid $x $y
set tracefd [open wireless.tr w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtracefd [open first.nam w]
$ns_ namtrace-all-wireless $namtracefd $x $y
set god_ [create-god $n]
$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 OFF
for {set i 0} { $i < $n} {incr i} {
set node_($i) [$ns_ node]
}
#TCP DATA TRAFFIC
#TCP Data Traffic
#create TCP source
set tcp0 [new Agent/TCP]
$ns_ attach-agent $node_(0) $tcp0
#create TCP destination
set sink0 [new Agent/TCPSink]
$ns_ attach-agent $node_(1) $sink0
#connect TCP src and destination
$ns_ connect $tcp0 $sink0
#create Application traffic
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
#Application start time
$ns_ at 1 "$ftp0 start"
#Application stop time
$ns_ at 10 "$ftp0 stop"

for {set i 0} {$i < $n} {incr i} {
$ns_ initial_node_pos $node_($i) 30
}
$ns_ at 100.0 "$ns_ halt"
#Run the simulation

$ns_ run

output


SCENARIO GENERATION
set x 500
set y 500
set n 20
set ns_ [new Simulator]
set topo [new Topography]
$topo load_flatgrid $x $y
set tracefd [open wireless.tr w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtracefd [open first.nam w]
$ns_ namtrace-all-wireless $namtracefd $x $y
set god_ [create-god $n]
$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 OFF
for {set i 0} { $i < $n} {incr i} {
set node_($i) [$ns_ node]
}
 #MOBILE NODE POSITION
#node position -static (fixed ) topology
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 150.0
$node_(1) set Z_ 0.0
#UDP DATA TRAFFIC
#UDP Data Traffic
#create UDP source
set udp0 [new Agent/UDP]
$ns_ attach-agent $node_(0) $udp0
#create UDP destination
set null0 [new Agent/Null]
$ns_ attach-agent $node_(1) $null0
#connect UDP src and destination
$ns_ connect $udp0 $null0
#create Application traffic
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.1
#MOBILE NODE MOVEMENT – MANUAL MOVEMENT
#Mobile Node Movement
$ns_ at 3.0 "$node_(1) setdest 450 400 50"
$ns_ at 10.0 "$node_(0) setdest 400 350 50"
source scen-20-10-20

#Application start time
$ns_ at 1 "$cbr0 start"
#Application stop time
$ns_ at 10 "$cbr0 stop"

for {set i 0} {$i < $n} {incr i} {
$ns_ initial_node_pos $node_($i) 30
}
$ns_ at 100.0 "$ns_ halt"
#Run the simulation
$ns_ run                              

scenario generation

To create 20 number of nodes

step 1: open new terminal
step 2: Go to the following directory
ur ns2 file where u intall so go to that model path
root home->ns-allinone 2.35->ns-2.35->indep-utils->cmu-scen-gen->setdest
  
in terminal window type cd ns-allinone 2.35/ns-2.35/indep-utils/cmu-scen-gen/setdest
then type the following format command
./setdest -n 20 -p 10 -M 20 -t 100 -x 500 -y 500 > scen-20-10-20

note 1:

-n number of nodes
-p pause time
-s maxspeed
-t simtime
-x max x
-y max y
scen-20-10-20 file name(our option)

note 2:after creating our file it copy file in our directory and in programs type source (file name give),number of nodes change it depends upon our needs

output:



CREATING RANDOM TRAFFIC-PATTERN FOR WIRELESS SCENARIOS
set x 500
set y 500
set n 20
set ns_ [new Simulator]
set topo [new Topography]
$topo load_flatgrid $x $y
set tracefd [open wireless.tr w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtracefd [open first.nam w]
$ns_ namtrace-all-wireless $namtracefd $x $y
set god_ [create-god $n]
$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 OFF
for {set i 0} { $i < $n} {incr i} {
set node_($i) [$ns_ node]
}
 #MOBILE NODE POSITION
#node position -static (fixed ) topology
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 150.0
$node_(1) set Z_ 0.0
#UDP DATA TRAFFIC
#UDP Data Traffic
#create UDP source
set udp0 [new Agent/UDP]
$ns_ attach-agent $node_(0) $udp0
#create UDP destination
set null0 [new Agent/Null]
$ns_ attach-agent $node_(1) $null0
#connect UDP src and destination
$ns_ connect $udp0 $null0
#create Application traffic
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.1
#MOBILE NODE MOVEMENT – MANUAL MOVEMENT
#Mobile Node Movement
$ns_ at 3.0 "$node_(1) setdest 450 400 50"
$ns_ at 10.0 "$node_(0) setdest 400 350 50"
source scen-20-10-20
source cbr-10nodes-3con

#Application start time
$ns_ at 1 "$cbr0 start"
#Application stop time
$ns_ at 10 "$cbr0 stop"

for {set i 0} {$i < $n} {incr i} {
$ns_ initial_node_pos $node_($i) 30
}
$ns_ at 100.0 "$ns_ halt"
#Run the simulation
$ns_ run
                     
                                 CBR GENERATION:

STEP1 :OPEN NEW TERMINAL
STEP2: TYPE FOLLOWING COMMAND
 cd ns-allinone 2.35/ns-2.35/indep-utils/cmu-scen-gen
NOW TYPE: ns cbrgen.tcl -type cbr -nn 10 -seed 1.0 -mc 3 -rate 4.0 > cbr-10nodes-3con
note 1:
-type cbr/tcp
-nn num of nodes
-seed seed
-mc max connections
-rate conn rate
cbr-10nodes-3con  file name options

note2:
after creating our file it copy file in our directory and in programs type source (file name give),number of nodes change it depends upon our needs.    
                       




No comments: