Adhoc
Adhoc
Adhoc
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.
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.
proc finish {} {
global ns nf
$ns flush-trace
close $nf
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
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]
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
}
proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog3b.nam &
exit 0
}
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
}
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)
$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
$ns flush-trace
close $nam
close $tracefd
close $stats
}
puts "Starting Simulation..."
$ns run
OUTPUT –
EXPERIMENT – 6
Aim- AODV simulation using NS2.
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
create-god $val(nn)
#
# Create nn mobilenodes [$val(nn)] and attach them to the channel.
#
# 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"
$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
#source 'thesis1.tcl';
#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"
}
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
# Node Creation
set node1 [$ns node]
# Initial color of the node
$node1 color black
#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
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
# Node Creation