Adhoc

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

INDEX

S.NO TOPIC DATE SIGN


1 Introduction to Network Simulator (NS2) and Simulatemesh topology
in NS2.
2 Simulate a four-node point-to-point network with thelinks connected
as follows:
n0 – n2, n1 – n2 and n2 – n3. Apply TCP agent between n0-n3 and
UDP between n1-n3. Apply relevant applications over TCP and
UDP agents changing the parameter and determine the number of
packets sent by TCP / UDP.
3 Simulate the different types of Internet traffic such asFTP and
TELNET over a network and analyse the throughput
4 Simulate a transmission of PING message over a network consisting of
six nodes and find the number ofpackets droped due to congestion.
5 Simulte Random MAC protocol using NS2.
6 AODV simulation using NS2.
7 MANET implementation in NS2 simulator.
8 VANET implementation in NS2 simulator.
9 NS2 simulation for wireless networks.
10 NS2 simulation for wireless sensor networks.
EXPERIMENT -1

Aim - Introduction to Network Simulator (NS2) and Simulate mesh topology in NS2.

INTRODUCTION
Network Simulator Version 2, widely known as NS2, is an event driven simulation tool that
is useful in studying the dynamic nature of communication networks. Simulation of wired as
well as wireless network functions and protocols (e.g., routing algorithms, TCP, UDP) can be
done using NS2. In general, NS2 provides users with a way of specifying such network
protocols and simulating their corresponding behaviours. Due to its flexibility and modular
nature, NS2 has gained constant popularity in the networking research community since its
birth in 1989. Ever since, several revolutions and revisions have marked the growing maturity
of the tool, thanks to substantial contributions from the players in the field. Among these are
the University of California and Cornell University who developed the REAL network
simulator,1 the foundation which NS is based on. Since 1995 the Defence Advanced
Research Projects Agency (DARPA) supported development of NS through the Virtual
InterNetwork Testbed (VINT) project.Currently the National Science Foundation (NSF) has
joined the ride in development. Last but not the least, the group of researchers and developers
in the community are constantly working to keep NS2 strong and versatile.

BASIC ARCHITECTURE OF NS2

Figure given below shows the basic architecture of NS2. NS2 provides users with an executable
command ns which takes on input argument, the name of a Tcl simulation scripting file. Users
are feeding the name of a Tcl simulation script (which sets up a simulation) as an input
argument of an NS2 executable command ns. In most cases, a simulation trace file is created,
and is used to plot graph and/or to create animation.
NS2 consists of two key languages: C++ and Object-oriented Tool Command Language (OTcl).
While the C++ defines the internal mechanism (i.e.,a back end) of the simulation objects, the OTcl
sets up simulation by assembling and configuring the objects as well as scheduling discrete events
(i.e.,a front end). The C++ and the OTcl are linked together using TclCL. Mapped to a C++ object,
variables in the OTcl domains are sometimes referred to as handles. Conceptually, a handle (e.g., n as
a Node handle) is just a string (e.g.,o10) in the OTcl domain, and does not contain any functionality.
Instead, the functionality(e.g., receiving a packet) is defined in the mapped C++ object (e.g., of class
Connector). In the OTcl domain, a handle acts as a front end which interacts with users and other
OTcl objects. It may defines its own procedures and variables to facilitate the interaction. Note that
the member procedures and variables in the OTcl domain are called instance procedures(instprocs)
and instance variables(instvars), respectively. Before proceeding further, the readers are encouraged to
learnC++ and OTcl languages and we include it on our previous posts.

NS2 provides a large number of built in C++ objects. It is advisable to use these C++ objects to set
up a simulation using a Tcl simulation script. However, advance users may find these objects
insufficient. They need to develop their own C++ objects, and use a OTcl configurationinterface to put
together these objects. After simulation, NS2 outputs either text-based or animation-based simulation
results. To interpret these results graphically and interactively, tools such as NAM (Network
AniMator) and XGraph are used. To analyze a particular behaviour of the network, users can extract
a relevant subset of text-based data and transformit to a more conceivable presentation.

Source code - Mesh Topology

set ns [new Simulator]


set nf [open out.nam w]
$ns namtrace-all $nf

proc finish {} {
global ns nf
$ns flush-trace
close $nf

exec nam out.nam &


exit0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n4 1Mb 10ms DropTail

$ns duplex-link $n1 $n3 1Mb 10ms DropTail


$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n4 1Mb 10ms DropTail

$ns duplex-link $n2 $n3 1Mb 10ms DropTail


$ns duplex-link $n2 $n4 1Mb 10ms DropTail

$ns duplex-link $n3 $n4 1Mb 10ms DropTail

$ns queue-limit $n0 $n2 1

set tcp0 [new Agent/TCP]


$tcp0 set class_ 1
$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n1 $sink0
$ns connect $tcp0 $sink0

set tcp0 [new Agent/TCP]


$tcp0 set class_ 1
$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n2 $sink0
$ns connect $tcp0 $sink0

set tcp0 [new Agent/TCP]


$tcp0 set class_ 1
$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
$ns connect $tcp0 $sink0

set tcp1 [new Agent/TCP]


$tcp1 set class_ 1
$ns attach-agent $n1 $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n2 $sink1
$ns connect $tcp1 $sink1

set tcp1 [new Agent/TCP]


$tcp1 set class_ 1
$ns attach-agent $n1 $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n3 $sink1
$ns connect $tcp1 $sink1

set tcp2 [new Agent/TCP]


$tcp1 set class_ 1
$ns attach-agent $n2 $tcp2
set sink1 [new Agent/TCPSink]
$ns attach-agent $n3 $sink1
$ns connect $tcp2 $sink1

set cbr0 [new Application/Traffic/CBR]


$cbr0 set packetSize_ 20
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0

set cbr1 [new Application/Traffic/CBR]


$cbr1 set packetSize_ 30
$cbr1 set interval_ 0.02
$cbr1 attach-agent $tcp1

set cbr2 [new Application/Traffic/CBR]


$cbr1 set packetSize_ 40
$cbr1 set interval_ 0.03
$cbr1 attach-agent $tcp2

$ns at 0.5 "$cbr0 start"


$ns at 4.5 "$cbr0 stop"

$ns at 1.5 "$cbr1 start"


$ns at 4.5 "$cbr1 stop"

$ns at 2.5 "$cbr1 start"


$ns at 4.5 "$cbr1 stop"

$ns at 5.0 "finish"


$ns run

OUTPUT-
EXPERIMENT -2

Aim -Simulate a four node point-to-point network with the links connected as follows:
n0 – n2, n1 – n2 and n2 – n3. Apply TCP agent between n0-n3 and UDP between n1-
n3.
Apply relevant applications over TCP and UDP agents changing the parameter and
determinethe number of packets sent by TCP / UDP.

Source Code

set ns [new Simulator]


set nf [open prog2.nam w]
$ns namtrace-all $nf
set nd [open prog2.tr w]
$ns trace-all $nd

proc finish {}
{global ns nf
nd
$ns flush-
traceclose $nf
exec nam prog2.nam
&exit 0
}

set n0 [$ns
node] set n1 [$ns
node] set n2 [$ns
node] set n3 [$ns
node]

$n0 label TCP


$n1 label UDP
$n3 label NULL-TCPSINK

$ns duplex-link $n0 $n2 1Mb 10ms DropTail


$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
$ns connect $tcp0 $sink0

set ftp0 [new Application/FTP]


$ftp0 attach-agent $tcp0

set udp0 [new Agent/UDP]


$ns attach-agent $n1
$udp0set null0 [new
Agent/Null]
$ns attach-agent $n3 $null0
$ns connect $udp0 $null0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

$ns at 0.2 "$cbr0 start"


$ns at 0.1 "$ftp0 start"
$ns at 4.5 "$cbr0 stop"
$ns at 4.4 "$ftp0 stop"

$ns at 5.0 "finish"


$ns run

OUTPUT –
EXPERIMENT – 3
Aim- Simulate the different types of Internet traffic such as FTP and TELNET over a
network and analyse the throughput.

prog3a.tcl
set ns [new Simulator]
set nf [open prog3a.nam w]
$ns namtrace-all $nf
set nd [open prog3a.tr w]
$ns trace-all $nd

proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog3a.nam &
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

$ns duplex-link $n0 $n1 1Mb 10ms DropTail


$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail

set tcp [new Agent/TCP]


set sink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

$ns at 0.2 "$ftp start"


$ns at 4.5 "$ftp stop"
$ns at 5.0 "finish"
$ns run
OUTPUT –
prog3b.tcl

set ns [new Simulator]


set nf [open prog3b.nam w]
$ns namtrace-all $nf
set nd [open prog3b.tr w]
$ns trace-all $nd

proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog3b.nam &
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

$ns duplex-link $n0 $n1 1Mb 10ms DropTail


$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail

set tcp0 [new Agent/TCP]


set tcpsink0 [new Agent/TCPSink]
$ns attach-agent $n0 $tcp0
$ns attach-agent $n4 $tcpsink0
set telnet0 [new Application/Telnet]
$telnet0 attach-agent $tcp0
$ns connect $tcp0 $tcpsink0

$ns at 0.2 "$telnet0 start"


$ns at 4.5 "$telnet0 stop"
$ns at 5.8 "finish"
$ns run

OUTPUT –
EXPERIMENT – 4
Aim- Simulate a transmission of PING message over a network consisting of six nodes and
find the number of packets dropped due to congestion.

Source Code
set ns [new Simulator]
set nf [open prog4.nam w]
$ns namtrace-all $nf
set nd [open prog4.tr w]
$ns trace-all $nd
proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog4.nam &
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]

$ns duplex-link $n1 $n0 1Mb 10ms DropTail


$ns duplex-link $n2 $n0 1Mb 10ms DropTail
$ns duplex-link $n3 $n0 1Mb 10ms DropTail
$ns duplex-link $n4 $n0 1Mb 10ms DropTail
$ns duplex-link $n5 $n0 1Mb 10ms DropTail
$ns duplex-link $n6 $n0 1Mb 10ms DropTail

Agent/Ping instproc recv {from rtt} {


$self instvar node_
puts "node [$node_ id] recieved ping answer from \
$from with round-trip-time $rtt ms."
}

set p1 [new Agent/Ping]


set p2 [new Agent/Ping]
set p3 [new Agent/Ping]
set p4 [new Agent/Ping]
set p5 [new Agent/Ping]
set p6 [new Agent/Ping]

$ns attach-agent $n1 $p1


$ns attach-agent $n2 $p2
$ns attach-agent $n3 $p3
$ns attach-agent $n4 $p4
$ns attach-agent $n5 $p5
$ns attach-agent $n6 $p6

$ns queue-limit $n0 $n4 3


$ns queue-limit $n0 $n5 2
$ns queue-limit $n0 $n6 2

$ns connect $p1 $p4


$ns connect $p2 $p5
$ns connect $p3 $p6

$ns at 0.2 "$p1 send"


$ns at 0.4 "$p2 send"
$ns at 0.6 "$p3 send"
$ns at 1.0 "$p4 send"
$ns at 1.2 "$p5 send"
$ns at 1.4 "$p6 send"
$ns at 2.0 "finish"
$ns run

Prog1.awk
BEGIN {count=0;
}
{
event=$1;
if(event=="d")
{
count++;
}
}
END {
printf("No of packets dropped : %d\n",count);
}
OUTPUT –
EXPERIMENT – 5
Aim- Simulate Random MAC protocol using NS2.

Source Code

# Project parameters
set val(node_num) 51
set val(duration) 10
set val(packetsize) 16
set val(repeatTx) 10
set val(interval) 0.02
set val(dimx) 50
set val(dimy) 50
set val(nam_file) "jinghaos_pa3.nam"
set val(trace_file) "jinghaos_pa3.tr"
set val(stats_file) "jinghaos_pa3.stats"
set val(node_size) 5

# Node 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/RMAC ;# MAC 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) $val(node_num) ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol

# Global variables
set ns [new Simulator]
set tracefd [open $val(trace_file) w]
set nam [open $val(nam_file) w]
set stats [open $val(stats_file) w]
$ns namtrace-all-wireless $nam $val(dimx) $val(dimy)
$ns trace-all $tracefd
set topo [new Topography]
$topo load_flatgrid $val(dimx) $val(dimy)

# Create God
create-god $val(nn)

#Mac/RMAC set repeatTx_ $val(repeatTx)


#Mac/RMAC set interval_ $val(interval)

$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 \
-movementTrace OFF

# The only sink node


set sink_node [$ns node]
$sink_node random-motion 0
$sink_node set X_ [expr $val(dimx)/2]
$sink_node set Y_ [expr $val(dimy)/2]
$sink_node set Z_ 0
$ns initial_node_pos $sink_node $val(node_size)

set sink [new Agent/LossMonitor]


$ns attach-agent $sink_node $sink

# Set up random number generator, to scatter the source nodes


set rng [new RNG]
$rng seed 0

set xrand [new RandomVariable/Uniform]


$xrand use-rng $rng
$xrand set min_ [expr -$val(dimx)/2]
$xrand set max_ [expr $val(dimx)/2]

set yrand [new RandomVariable/Uniform]


$yrand use-rng $rng
$yrand set min_ [expr -$val(dimy)/2]
$yrand set max_ [expr $val(dimy)/2]

set trand [new RandomVariable/Uniform]


$trand use-rng $rng
$trand set min_ 0
$trand set max_ $val(interval)
# Create all the source nodes
for {set i 0} {$i < $val(nn)-1 } {incr i} {
set src_node($i) [$ns node]
$src_node($i) random-motion 0
set x [expr $val(dimx)/2 + [$xrand value]]
set y [expr $val(dimx)/2 + [$xrand value]]
$src_node($i) set X_ $x
$src_node($i) set Y_ $y
$src_node($i) set Z_ 0
$ns initial_node_pos $src_node($i) $val(node_size)

set udp($i) [new Agent/UDP]


$udp($i) set class_ $i
$ns attach-agent $src_node($i) $udp($i)
$ns connect $udp($i) $sink

set cbr($i) [new Application/Traffic/CBR]


$cbr($i) set packet_size_ $val(packetsize)
$cbr($i) set interval_ $val(interval)
$cbr($i) attach-agent $udp($i)
set start [$trand value]
$ns at $start "$cbr($i) start"
$ns at $val(duration) "$cbr($i) stop"
}
proc stop {} {
global ns tracefd nam stats val sink

set bytes [$sink set bytes_]


set losts [$sink set nlost_]
set pkts [$sink set npkts_]
puts $stats "bytes losts pkts"
puts $stats "$bytes $losts $pkts"

$ns flush-trace
close $nam
close $tracefd
close $stats
}
puts "Starting Simulation..."
$ns run
OUTPUT –
EXPERIMENT – 6
Aim- AODV simulation using NS2.

AODV referred as Ad Hoc On-Demand Distance Vector.It is routing protocol which is


designed for wireless and mobile ad hoc network.AODV Protocol establishes route with
destination only when it is required.AODV Protocol supports both unicast and multicast
routing protocol.

Source Code

# 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) 12 ;# 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) 150 ;# time of simulation end

set ns [new Simulator]


set tracefd [open testAODV.tr w]
set windowVsTime2 [open win.tr w]
set namtrace [open testAODV.nam w]

$ns trace-all $tracefd


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

# set up topography object


set topo [new Topography]

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

create-god $val(nn)

#
# Create nn mobilenodes [$val(nn)] and attach them to the channel.
#

# 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

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


set node_($i) [$ns node]
$node_($i) set X_ [ expr 10+round(rand()*480) ]
$node_($i) set Y_ [ expr 10+round(rand()*380) ]
$node_($i) set Z_ 0.0
}

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


$ns at [ expr 15+round(rand()*60) ] "$node_($i) setdest [ expr 10+round(rand()*480) ] [
expr 10+round(rand()*380) ] [ expr 2+round(rand()*15) ]"

# Generation of movements
# $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
# $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
# $ns at 70.0 "$node_(2) setdest 480.0 300.0 5.0"
# $ns at 20.0 "$node_(3) setdest 200.0 200.0 5.0"
# $ns at 25.0 "$node_(4) setdest 50.0 50.0 10.0"
# $ns at 60.0 "$node_(5) setdest 150.0 70.0 2.0"
# $ns at 90.0 "$node_(6) setdest 380.0 150.0 8.0"
# $ns at 42.0 "$node_(7) setdest 200.0 100.0 15.0"
# $ns at 55.0 "$node_(8) setdest 50.0 275.0 5.0"
# $ns at 19.0 "$node_(9) setdest 250.0 250.0 7.0"
# $ns at 90.0 "$node_(10) setdest 150.0 150.0 20.0"

# Set a TCP connection between node_(2) and node_(8)


set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $node_(2) $tcp
$ns attach-agent $node_(8) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"
set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $node_(5) $tcp
$ns attach-agent $node_(0) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"

# Printing the window size


proc plotWindow {tcpSource file} {
global ns
set time 0.01
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 10.1 "plotWindow $tcp $windowVsTime2"

# 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
}

# Telling nodes when the simulation ends


for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "$node_($i) reset";
}

# ending nam and the simulation


$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 150 "puts \"end simulation\" ; $ns halt"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
}

$ns run
OUTPUT –
EXPERIMENT – 7
Aim- MANET implementation in NS2 simulator.

Unlike infrastructure based wireless networks, a mobile ad hoc network or MANET does not
depend on a fixed infrastructure for its networking operation. MANET is an autonomous and
short-lived association of group of mobile nodes that communicate with each other over
wireless links. A node can directly communicate to the nodes that lie within its
communication range. If a node wants to communicate with a node that is not directly within
its communication range, it uses intermediate nodes as routers.
Source Code
# 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) 20 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 1000 ;# X dimension of topography
set val(y) 1000 ;# Y dimension of topography
set val(stop) 150 ;# time of simulation end

set ns [new Simulator]


set tracefd [open thesis.tr w]
set namtrace [open thesis.nam w]

$ns trace-all $tracefd


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

# set up topography object


set topo [new Topography]

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


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 ON \
-movementTrace ON

# Energy model for every node


#Energy=Power*time
#Jule is unit of initialEnergy and rest of Energy is unit is watt
$ns node-config -energyModel EnergyModel \
-initialEnergy 20 \
-txPower 0.744 \
-rxPower 0.0648 \
-idlePower 0.05 \
-sensePower 0.0175

#source 'thesis1.tcl';

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


set n($i) [$ns node]
}

$n(0) set initialEnergy 5


$n(1) set initialEnergy 25
$n(2) set initialEnergy 15
# Provide initial location of mobilenodes

$n(0) set X_ 105.0


$n(0) set Y_ 206.0
$n(0) set Z_ 0.0

$n(1) set X_ 805.0


$n(1) set Y_ 742.0
$n(1) set Z_ 0.0

$n(2) set X_ 515.0


$n(2) set Y_ 606.0
$n(2) set Z_ 0.0

$n(3) set X_ 405.0


$n(3) set Y_ 342.0
$n(3) set Z_ 0.0

$n(4) set X_ 675.0


$n(4) set Y_ 156.0
$n(4) set Z_ 0.0

$n(5) set X_ 905.0


$n(5) set Y_ 549.0
$n(5) set Z_ 0.0

$n(6) set X_ 675.0


$n(6) set Y_ 336.0
$n(6) set Z_ 0.0

$n(7) set X_ 555.0


$n(7) set Y_ 762.0
$n(7) set Z_ 0.0

$n(8) set X_ 258.0


$n(8) set Y_ 646.0
$n(8) set Z_ 0.0

$n(9) set X_ 645.0


$n(9) set Y_ 522.0
$n(9) set Z_ 0.0

$n(10) set X_ 265.0


$n(10) set Y_ 376.0
$n(10) set Z_ 0.0

$n(11) set X_ 495.0


$n(11) set Y_ 502.0
$n(11) set Z_ 0.0

$n(12) set X_ 75.0


$n(12) set Y_ 426.0
$n(12) set Z_ 0.0

$n(13) set X_ 145.0


$n(13) set Y_ 119.0
$n(13) set Z_ 0.0

$n(14) set X_ 85.0


$n(14) set Y_ 702.0
$n(14) set Z_ 0.0

$n(15) set X_ 340.0


$n(15) set Y_ 36.0
$n(15) set Z_ 0.0

$n(16) set X_ 758.0


$n(16) set Y_ 602.0
$n(16) set Z_ 0.0

$n(17) set X_ 435.0


$n(17) set Y_ 186.0
$n(17) set Z_ 0.0
$n(18) set X_ 5.0
$n(18) set Y_ 2.0
$n(18) set Z_ 0.0

$n(19) set X_ 515.0


$n(19) set Y_ 366.0
$n(19) set Z_ 0.0

# Set a TCP connection between n(1) and n(13)


set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $n(1) $tcp
$ns attach-agent $n(13) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"

# Set a TCP connection between n(13) and n(7)


set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $n(13) $tcp
$ns attach-agent $n(7) $sink
$ns connect $tcp $sink

#defining heads
$ns at 0.0 "$n(0) label CH"
$ns at 0.0 "$n(1) label Source"
$ns at 0.0 "$n(13) label Destination"
#$ns at 0.0 "$n(2) label N2"

#new location after move


$ns at 0.0 "$n(0) setdest 130.0 208.0 5.0"
$ns at 0.0 "$n(1) setdest 485.0 128.0 5.0"
$ns at 1.0 "$n(2) setdest 615.0 340.0 5.0"
$ns at 1.0 "$n(3) setdest 680.0 458.0 5.0"
$ns at 3.0 "$n(4) setdest 580.0 368.0 5.0"
$ns at 3.0 "$n(5) setdest 785.0 228.0 5.0"
$ns at 2.0 "$n(6) setdest 750.0 638.0 5.0"
$ns at 1.0 "$n(7) setdest 185.0 120.0 5.0"
$ns at 0.0 "$n(8) setdest 335.0 700.0 5.0"
$ns at 2.0 "$n(9) setdest 425.0 590.0 5.0"
$ns at 2.0 "$n(10) setdest 105.0 620.0 5.0"
$ns at 0.0 "$n(11) setdest 565.0 420.0 5.0"
$ns at 1.0 "$n(12) setdest 700.0 20.0 5.0"
$ns at 1.0 "$n(13) setdest 115.0 85.0 5.0"
$ns at 1.0 "$n(14) setdest 195.0 185.0 5.0"
$ns at 1.0 "$n(15) setdest 387.0 590.0 5.0"
$ns at 2.0 "$n(16) setdest 165.0 620.0 5.0"
$ns at 0.0 "$n(17) setdest 765.0 320.0 5.0"
$ns at 1.0 "$n(18) setdest 109.0 20.0 5.0"
$ns at 1.0 "$n(19) setdest 175.0 185.0 5.0"

# Define node initial position in nam


for {set i 0} {$i < $val(nn)} { incr i } {
# 40 defines the node size for nam
$ns initial_node_pos $n($i) 60
}

# Telling nodes when the simulation ends


for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "$n($i) reset";
}
# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 150.01 "puts \"end simulation\" ; $ns halt"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam thesis.nam &

}
proc finish {} {
source shortest_path.tcl
mkMatrix
exec xgraph thesis.tr -geometry 800x400 &

exit 0
}

$ns run

OUTPUT –
EXPERIMENT – 8
Aim- VANET implementation in NS2 simulator.

Vehicular Ad hoc Network (VANET) are treated as mobile sensor networks and
characterized with special characteristics such as high node mobility and rapid topology
changes. VANET nodes can sense a variety of data in its surrounding area to offer several
services including traffic monitoring, speed controlling, lost vehicle locating and
environmental monitoring as it covers permanently a wide geographical region. Nodes are
configured with different communication. Vehicles moves within the specified network
boundary. Nodes in VANET can communicate in two ways: vehicle-to-vehicle (V2V)
communication and Vehicle-to-infrastructure (V2I) communication. In V2I communication
model, vehicles communicate to Road-Side-Unit (RSU) through Road-Side-Routers. Data
Transmission is established between nodes using UDP agent and CBR traffic. The sample
19.tcl designs a VANET with sensor node configuration, communication model, mobility
model, and energy model components.

Source Code
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif1) Phy/WirelessPhy ;# network interface type
set val(netif2) 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) 46 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 200 ;# X dimension of topography
set val(y) 200 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end
# Simulator Instance Creation
set ns [new Simulator]
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# general operational descriptor- storing the hop details in the network
create-god $val(nn)
# For model 'TwoRayGround'
set dist(20m) 4.80696e-07
set dist(26m) 2.84435e-07
set dist(27m) 2.63756e-07
set dist(28m) 2.45253e-07
set dist(25m) 3.07645e-07
set dist(30m) 2.13643e-07
set dist(35m) 1.56962e-07
set dist(50m) 7.69113e-08
set dist(75m) 3.41828e-08
set dist(60m) 5.34106e-08
set dist(70m) 3.92405e-08
# unity gain, omni-directional antennas
# set up the antennas to be centered in the node and 1.5 meters above it
Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1.0
Antenna/OmniAntenna set Gr_ 1.0
# Initialize the SharedMedia interface with parameters to make
# it work like the 914MHz Lucent WaveLAN DSSS radio interface
$val(netif1) set CPThresh_ 10.0
$val(netif1) set CSThresh_ $dist(70m)
$val(netif1) set RXThresh_ $dist(75m)
$val(netif1) set Rb_ 2*1e6
$val(netif1) set Pt_ 0.2818
$val(netif1) set freq_ 914e+6
$val(netif1) set L_ 1.0
$val(netif2) set CPThresh_ 10.0
$val(netif2) set CSThresh_ $dist(75m)
$val(netif2) set RXThresh_ $dist(75m)
$val(netif2) set Rb_ 2*1e6
$val(netif2) set Pt_ 0.2818
$val(netif2) set freq_ 914e+6
$val(netif2) set L_ 1.0
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# Create God
set god_ [create-god $val(nn)]
set chan_1_ [new $val(chan)]
# configure node
$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(netif1) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON \
-channel $chan_1_ \
for {set i 0} {$i < 1 } {incr i} {
set node_($i) [$ns node]
$node_($i) color black
$node_($i) random-motion 0 ;# disable random motion
}
$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(netif2) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON \
-channel $chan_1_ \
for {set i 1} {$i < 4 } {incr i} {
set node_($i) [$ns node]
$node_($i) color black
}
OUTPUT –
EXPERIMENT – 9
Aim- NS2 simulation for wireless networks.

The wireless networking model can be created using Tool Command Language (TCL) script
with fixed number of nodes. The sample code discussed below models the wireless network
with 2 nodes. Nodes are configured with the components of channel, networking interface,
radio propagation model, Medium Access Control (MAC) protocol, adhoc routing protocol,
interface queue, link layer, topography object, and antenna type.
Source Code

# Simulator Instance Creation


set ns [new Simulator]

#Fixing the co-ordinate of simutaion 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 sample1.nam w]
#Tracing all the events and cofiguration
$ns namtrace-all-wireless $namfile $val(x) $val(y)

#Trace File creation


set tracefile [open sample1.tr w]

#Tracing all the events and cofiguration


$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 30
$ns initial_node_pos $node2 30
# 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 sample1.nam &
}

#Starting scheduler
$ns run
OUTPUT –
EXPERIMENT – 10
Aim- NS2 simulation for wireless sensor networks.

A wireless sensor network (WSN) consists of a large number of small sensor nodes that are
deployed in the area in which a factor is to be monitored. In wireless sensor network, energy
model is one of the optional attributes of a node. The energy model denotes the level of
energy in a mobile node. The components required for designing energy model includes
initialEnergy, txPower, rxPower, and idlePower. The “initialEnergy” represents the level of
energy the node has at the initial stage of simulation. “txPower” and “rxPower” denotes the
energy consumed for transmitting and receiving the packets. If the node is a sensor, the
energy model should include a special component called “sensePower”. It denotes the energy
consumed during the sensing operation. Apart from these components, it is important to
specify the communication range (RXThresh_) and sensing range of a node (CSThresh_).
The sample 18.tcl designs a WSN in which sensor nodes are configured with different
communication and sensing range. Base Station is configured with highest communication
range. Data Transmission is established between nodes using UDP agent and CBR traffic.
Source Code

# Simulator Instance Creation


set ns [new Simulator]

#Fixing the co-ordinate of simulation area


set val(x) 600
set val(y) 600
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif1) Phy/WirelessPhy ;# network interface type
set val(netif2) 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) 10 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 600 ;# X dimension of topography
set val(y) 600 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end
set val(energymodel) EnergyModel ;#Energy set up
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

# general operational descriptor- storing the hop details in the network


create-god $val(nn)

#Transmission range setup

#********************************** UNITY GAIN, 1.5m HEIGHT OMNI


DIRECTIONAL ANTENNA SET UP **************

Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1.0
Antenna/OmniAntenna set Gr_ 1.0

#********************************** SET UP COMMUNICATION AND SENSING


RANGE ***********************************

#default communication range 250m

# Initialize the SharedMedia interface with parameters to make


# it work like the 914MHz Lucent WaveLAN DSSS radio interface
$val(netif1) set CPThresh_ 10.0
$val(netif1) set CSThresh_ 2.28289e-11 ;#sensing range of 500m
$val(netif1) set RXThresh_ 2.28289e-11 ;#communication range of 500m
$val(netif1) set Rb_ 2*1e6
$val(netif1) set Pt_ 0.2818
$val(netif1) set freq_ 914e+6
$val(netif1) set L_ 1.0

# Initialize the SharedMedia interface with parameters to make


# it work like the 914MHz Lucent WaveLAN DSSS radio interface
$val(netif2) set CPThresh_ 10.0
$val(netif2) set CSThresh_ 8.91754e-10 ;#sensing range of 200m
$val(netif2) set RXThresh_ 8.91754e-10 ;#communication range of 200m
$val(netif2) set Rb_ 2*1e6
$val(netif2) set Pt_ 0.2818
$val(netif2) set freq_ 914e+6
$val(netif2) set L_ 1.0

# configure the first 5 nodes with transmission range of 500m

$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(netif1) \
-channelType $val(chan) \
-topoInstance $topo \
-energyModel $val(energymodel) \
-initialEnergy 10 \
-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3 \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

# Node Creation

set energy(0) 1000

$ns node-config -initialEnergy 1000 \


-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3

set node_(0) [$ns node]


$node_(0) color black
# configure the remaining 5 nodes with transmission range of 200m

$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(netif2) \
-channelType $val(chan) \
-topoInstance $topo \
-energyModel $val(energymodel) \
-initialEnergy 10 \
-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3 \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
for {set i 1} {$i < 3} {incr i} {

set energy($i) [expr rand()*500]

$ns node-config -initialEnergy $energy($i) \


-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3
set node_($i) [$ns node]
$node_($i) color black}
OUTPUT –

You might also like