Sie sind auf Seite 1von 40

NETWORK SIMULATOR 2

 Introduction
 NS2
 NAM
 Languages
 Cygwin
 About Cygwin
 Installing Cygwin
 Installing NS2
 Sample script
 Event driven network simulator

 Developed at UC Berkely

 Implements network protocols


 TCP, UPD
 Traffic source mechanism: FTP, TELNET, Web, CBR and
VBR
 Router Queue management mechanism: Drop Tail, RED,
CBQ
 Routing Algorithm: Dijkstra, and etc…
 Multicasting, Mac layer protocols for LAN
• NS2 is event simulator
– It depends on the timing of events which are maintained by a
scheduler.
• Event
– An object
– Unique ID
– Scheduled time
– Pointer to an object

• Scheduler
– Maintains the order of the events to be executed and fires them
one by one
• NAM works with NS to form a very power tool

• NS contains protocols

• With NAM these protocols can visualized as


animations

• For example
– Visualize packet flow
– Protocol behavior, state etc
• TCL – Tool Command Language

• OTCL – Object Oriented TCL

• NS is basically an OTCL interpreter with


network simulation object libraries (as we
have compiler in C language)
• Based on two languages
– C++
– OTCL

• Has rich library of network and protocol


object

• With OTCL script provided by user, a network


topology is simulated
• NS 2 is a Linux based simulator but it can be
installed on Windows

• To install it in Windows environment we have


to install Cygwin

• Cygwin is a Windows based utility that


provides the Linux like environment in
Windows
• Run setup
• Select “Install from local directory” and give the
paths of packages
• In select packages we must include the following
after clicking on the view button
– XFree86-base, XFree86-bin, XFree86-prog, XFree86-
lib, XFree86-etc, make, patch, perl, gcc, gcc-g++,
gawk, gnuplot, gzip and tar
– To select, click “skip” button
• Copy the ns-allinone-2.27.tar.gz package file
in home\ user directory (for example c:\
cygwin\ home\ SANA)

• Run cygwin and type


– tar xvfz ns-allinone-2.27.tar.gz
– After file extraction type cd ns-allinone-2.27
– ./install
– (Press y if any error occurred)
• To set environment variables type
export NS_HOME=`pwd`
export PATH=$NS_HOME/tcl8.4.5/unix:
$NS_HOME/tk8.4.5/unix:$NS_HOME/bin:$PATH
export LD_LIBRARY_PATH=$NS_HOME/tcl8.4.5/unix:
$NS_HOME/tk8.4.5/unix:\
$NS_HOME/otcl-1.8:$NS_HOME/lib:$LD_LIBRARY_PATH
export TCL_LIBRARY=$NS_HOME/tcl8.4.5/library

You have to set this each time you run cygwin


 Go to your NS2 directory
 e.g C:\cygwin\home\SANA\ns-allinone-2.27\ns-
2.27

 Make a new folder named ‘work’

 Create new file and name it ‘prog1.tcl’

 Open the file in word pad


 Now we are going to write a template that
we can use for all of the first Tcl scripts

 We can write our Tcl scripts in any text editor


like word pad or notepad

 Lets start writing our first Tcl Script


 Create a simulator object
 set ns [new Simulator]

 Open a file for writing that is going to be used for the nam
trace data
 set nf [open out.nam w]
 Opens the file 'out.nam' for writing and gives it the file
handle 'nf’
 $ns namtrace-all $nf
 This will tell the simulator object that we created above to
write all simulation data that is going to be relevant for
nam into this file.
 Add a 'finish' procedure that closes the trace file
and starts nam

proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
 Execute 'finish' procedure after 5.0 seconds of
simulation time
 $ns at 5.0 "finish“

 Start the simulation


 $ns run

 Here the template ends. It is used in mostly


all the programs that you write. Lets have a
look at the template again.
#Create a simulator object
set ns [new Simulator]

#Open the nam trace file


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

#Define a 'finish' procedure


proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
 Now we are going to define a very simple topology
with two nodes that are connected by a link

 Create 2 nodes
 Syntax : set node_name [$ns node]
 set n0 [$ns node]
 set n1 [$ns node]

 A new node object is created with the command


'$ns node'. The above code creates two nodes and
assigns them to the handles 'n0' and 'n1'.
 Create link between two nodes
 Syntax :
 $ns link_type $node1_handle $node2_handle
Bandwidth Delay Queing_Protocol

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

 It connects the nodes n0 and n1 with a duplex


link with the bandwidth 1Megabit/sec, delay
of 10ms and DropTail queuing protocol
 Now save your file

 Note: You have to insert the code in the


template before the line '$ns at 5.0 "finish"‘

 Your final code should be like this.


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 &
exit 0
}
#-------------------------------------------------#
set n0 [$ns node]
set n1 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
#-------------------------------------------------#
$ns at 5.0 "finish“
$ns run
 Execute your first program

 Type (in cygwin)


 startxwin.bat
 cd ns-2.27/work
 ns prog1.tcl

 This will simulate your first program on NAM

 Click play button to view the animation

Das könnte Ihnen auch gefallen