Download - Ns2: Introduction - Part I
![Page 1: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/1.jpg)
Ajit K Nayak, Ph.D.Dept of CSIT, ITER
An Introduction to NS2:
The Network Simulator
![Page 2: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/2.jpg)
What is Simulation ?
Network simulation is a technique where a program models the behavior
of a network either by calculating the interaction between the different
network entities (hosts/, etc.) using mathematical formulas, or actually
capturing and playing back observations from a production network.
![Page 3: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/3.jpg)
NS2 Architecture
![Page 4: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/4.jpg)
Sample Input Script (Tcl)
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
$ns at 0.0 "$n0 label Sender“
$ns at 0.0 "$n1 label Receiver“
set nf [open test.nam w]
$ns namtrace-all $nf
set f [open test.tr w]
$ns trace-all $f
$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail
$ns duplex-link-op $n0 $n1 orient right
. . .
![Page 5: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/5.jpg)
Sample output traces- 2.846272 0 2 ack 576 ------- 0 0.0 4.0 15545 61
r 2.851664 0 2 ack 576 ------- 0 0.0 4.0 14473 59
+ 2.851664 2 3 ack 576 ------- 0 0.0 4.0 14473 59
d 2.851664 2 3 ack 576 ------- 0 0.0 4.0 14473 59
r 2.854576 3 2 ack 40 ------- 1 4.0 0.0 3217 48
n -t * -s 0 -x 200 -y 30 -Z 0 -z 30 -v circle -c black
c -t * -i 8 -n red
c -t * -i 1 -n black
c -t * -i 2 -n orange
c -t * -i 6 -n tan
c -t * -i 7 -n purple
c -t * -i 3 -n green
+ -t 0.001635381 -s 0 -d -1 -p message -e 90 -c 2 -a 0 -i 0 -k MAC
- -t 0.001635381 -s 0 -d -1 -p message -e 90 -c 2 -a 0 -i 0 -k MAC
h -t 0.001635381 -s 0 -d -1 -p message -e 90 -c 2 -a 0 -i 0 -k MAC
r -t 0.002355848 -s 2 -d -1 -p message -e 32 -c 2 -a 0 -i 0 -k MAC
![Page 6: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/6.jpg)
Output (NAM Trace)
Run the
NAM trace
using
Network
AniMator
package
![Page 7: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/7.jpg)
Output (Packet Trace)
Extract required data from packet trace (shell, awk, perl, C, etc…)
Plot the data using gnuPlot, Xgraph, MsExcel etc.
![Page 8: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/8.jpg)
Where to go?
Learn Tcl/OTcl
Write Tcl scripts for network simulation
Execute the simulation
Observe the result in NAM
Learn to extract Info from Packet trace
Plot using any plotting software
Measure the protocol/network performance
![Page 9: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/9.jpg)
Tcl/OTcl - Basics Everything in Tool comand language(Tcl) is a command
followed by a number of arguments, which are separated by whitespace. command arg1 arg2 ... argn.
To Execute a Tcl script
Method 1:
Start the NS environment
$ ns (this will start the shell)
%
% puts stdout “Hello World”Hello World
%
Method 2:
Write the Tcl command(s) in a file and save it to <fileName>.tcl, then execute as follows in (OS shell)
$ ns <fileName>.tcl
![Page 10: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/10.jpg)
Tcl/OTcl – I/O Commands Printing(output): puts
puts $<var>
puts <constant>
Reading(input): gets
gets stdin <var>
Comment: #
# This is my first tcl script
![Page 11: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/11.jpg)
Tcl/OTcl - Variables Tcl is a dynamically typed language
Need not to declare variable types (datatypes).
A variable can store anything, irrespective of type and type checking is done at runtime.
Variable initialization.
Syntax: set <variable> <value>
Example:
% set intVar 10
% set a [expr 2.5*10]
% puts {intVar is $intVar}
% puts $a
![Page 12: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/12.jpg)
Tcl/OTcl – control structure-IControl Structures:
if {$z == 6} then { puts “Correct!”}
for {set i =0} {$i < 5} {incr i } {
puts “$i * $i equals [expr $i * $i]”
}
Other branching-constructsswitch <string> {
<pattern_1> {
<body_1>
}
<pattern_2> {
<body_2>
}
. . .
<default> {
body
}
}
![Page 13: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/13.jpg)
Tcl/OTcl – control structure-IIOther Loop constructs
while {<test>} {
<body>
}
foreach {<var1> <var2>} <var> {
<body>
}
Example:
set observations {Bhubaneswar 35 49 Cuttack 32 45 Bolangir 18 30}
foreach {town Tmin Tmax} $observations {
set Tavg [expr ($Tmin+$Tmax)/2.0]
puts “$town $Tavg"
}
break, continue
![Page 14: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/14.jpg)
Tcl/OTcl – ArrayRequired to store multiple elements. These are
implemented as associative maps.
i.e. it associates an array index (key) with an array element. Unlike
‘C/C++’ arrays, the Tcl array index can be a string.
Exampleset myArray(0) 1
set myArray(1) 65
set Link(pktsize) 512
set Link(protocol) “IEEE 802.11"
puts $myArray(0)
puts $myArray(1)
puts $Link(pktsize)
puts $Link(protocol)
![Page 15: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/15.jpg)
Tcl/OTcl – ProcedureThe ‘proc’ command creates a new command.
Syntax: proc <procName> <args> <body>
Example:
# procedure without args
proc myCommand {} {
puts “My first Tcl command"
}
myCommand #proc call
# procedure with args
proc avg {min max} {
return [expr ($min + $max) / 2]
}
set average [avg 10 20]; puts $average #proc call
![Page 16: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/16.jpg)
Tcl – Practice Progs.Write Tcl scripts for the followings
to add two given numbers.
To find largest among three numbers
To print a day given a number (1-7) using switch
To add all odd numbers (1-100)
To read and print one array containing name and
roll numbers of some students.
Write a procedure to find the sum of the digits of a
given number. Use this procedure to find sum of
the digits for a given range of numbers.
![Page 17: Ns2: Introduction - Part I](https://reader034.vdocuments.us/reader034/viewer/2022051404/5aaa32847f8b9a75178b4653/html5/thumbnails/17.jpg)
End of NS2 Part I
Thank You