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

Networking Lab Assignment 19

Network simulator NS-2

Albin Antony
10 February 2019
1 Network simulator NS-2
1.1 Aim
Install network simulator NS-2 in any of the Linux operating system and simu-
late wired and wireless scenarios.

1.2 Theory
1.2.1 NS-2
NS2 is an open-source simulation tool that runs on Linux. It is a discreet event
simulator targeted at networking research and provides substantial support for
simulation of routing, multicast protocols and IP protocols, such as UDP, TCP,
RTP and SRM over wired and wireless (local and satellite) networks. It has
many advantages that make it a useful tool, such as support for multiple proto-
cols and the capability of graphically detailing network traffic. Additionally, NS2
supports several algorithms in routing and queuing. LAN routing and broad-
casts are part of routing algorithms. Queuing algorithms include fair queuing,
deficit round-robin and FIFO.

1.2.2 Installation
Step 1: Download ns2 from here The package downloaded will be named ”ns-
allinone-2.35.tar.gz”. Copy it to the home folder. Then in a terminal use the
following two commands to extract the contents of the package.:
cd ~/
tar -xvzf ns-allinone-2.35.tar.gz

Step 2:Building the dependencies


sudo apt-get install build-essential autoconf automake libxmu-dev
sudo apt-get install gcc-4.4

Now open the file named ”ls.h” and scroll to the 137th line. In that change the
word ”erase” to ”this->erase”. The image below shows the line 137 (highlighted
in the image below) after making the changes to the ls.h file.To open the file
use the following command:
cd ~/ns-allinone-2.35/ns-2.35/linkstate
gedit ls.h

Now there is one more step that has to be done. We have to tell the ns which
version of GCC will be used. To do so, go to your ns folder and type the
following command:

1
sudo gedit ns-allinone-2.34/otcl-1.13/Makefile.in

In the file, change Change CC= @CC@ to CC=gcc-4.4, as shown in the image
below.
Step 3:Installation

sudo su cd ~/ns-allinone-2.35/./install

Step 4:Setting up the environment path The final step is to tell the system,
where the files for ns2 are installed or present. To do that, we have to set the
environment path using the ”.bashrc” file. In that file, we need to add a few lines
at the bottom. The things to be added are given below. But for the path indi-
cated below, many of those lines have ”/home/albin/ns-allinone-2.35/....” , but
that is where I have my extracted folder. Make sure you replace them with your
path. For example, if you have installed it in a folder ”/home/abc”, then re-
place ”/home/albin/ns-allinone-2.35/otcl-1.14” with ”/home/abc/ns-allinone-
2.35/otcl-1.14”.
Step 5:Running NS-2
ns

1.2.3 TCL
Tcl (pronounced ”tickle” or tee cee ell, /ti si l/) is a high-level, general-purpose,
interpreted, dynamic programming language. It was designed with the goal of
being very simple but powerful.Tcl casts everything into the mold of a com-
mand, even programming constructs like variable assignment and pro- cedure
definition.

1.2.4 C++
NS2 uses OTcl to create and configure a network, and uses C++ to run simula-
tion.

2
1.3 Program
1.3.1 Wired Network Simulation

# Create s c h e d u l e r
set ns [ new S i m u l a t o r ]

$ns color 1 darkmagenta


$ns color 2 yellow
$ns color 3 blue
$ns color 4 green
$ns color 5 black

#T r a c i n g s i m u l a t i o n r e s u l t s
set f i n [ open r e s u l t . d a t w ]
$ns t r a c e − a l l $ f i n

#T r a c i n g f o r NAM( Network Animator )


set n f i n [ open out.nam w ]
$ns n a m t r a c e − a l l $ n f i n
# C r e a t e a node
set n0 [ $ns node ]

# C r e a t e a n o t h e r node
set n1 [ $ns node ]
# C r e a t e TCP a g e n t and a t t a c h t o f i r s t node
set t c p 0 [ new Agent /TCP]
$ns attach−agent $n0 $ t c p 0

# C r e a t e TCP r e c e i v e r and a t t a c h t o s e c o n d node


set t c p 1 [ new Agent /TCPSink ]
$ns attach−agent $n1 $ t c p 1
# Connect both nodes with 1 .5Mbps bandwidth, 5 m i l l i s e c o n d s d e l a y and DropTai
$ns d u p l e x − l i n k $n0 $n1 1Mb 5ms DropTail

# Connect t c p 0 and t c p 1 a g e n t s
$ns c o n n e c t $ t c p 0 $ t c p 1

$ t c p 0 set f i d 4
$ t c p 1 set f i d 2
# C r e a t e a FTP o b j e c t
set f t p [ new A p p l i c a t i o n /FTP]
# Attach f t p a p p l i c a t i o n with a g e n t t c p 0
$ f t p attach−agent $ t c p 0
$ f t p set f i d 3

3
# Schedule events
$ns a t 0 . 2 ” $ f t p s t a r t ”
$ns a t 2 . 0 ” $ f t p s t o p ”
# F i n i s h p r o c e d u r e t o perform o p e r a t i o n a t t h e end o f s i m u l a t i o n

proc f i n i s h { } {
# Mapping o f global variable i n t o l o c a l variable
global ns f i n n f i n
# Flush a l l b u f f e r s
$ns f l u s h − t r a c e
#c l o s e f i l e o b j e c t s
close $ f i n
close $ n f i n
#Execute nam p r o c e s s i n background
exec nam out.nam &
#Terminate c u r r e n t p r o c e s s
exit 0
}
$ns a t 2 . 2 ” f i n i s h ”
$ns run

Figure 1: Wired Network

1.3.2 Wireless Network Simulation

#i n i t i a l i z e t h e v a r i a b l e s

4
set v a l ( chan ) Channel / W i r e l e s s C h a n n e l ;# Channel Type
set v a l ( prop ) P r o p a g a t i o n /TwoRayGround ;# r a d i o − p r o p a g a t i o n model
set v a l ( n e t i f ) Phy/ WirelessPhy ;# network i n t e r f a c e t y p e WAV
set v a l ( mac ) Mac/802 1 1 ;# MAC t y p e
set v a l ( i f q ) Queue/ DropTail / PriQueue ;# i n t e r f a c e queue t y p e
set v a l ( l l ) LL ;# l i n k l a y e r t y p e
set v a l ( ant ) Antenna /OmniAntenna ;# antenna model
set v a l ( i f q l e n ) 50 ;# max p a c k e t i n i f q
set v a l ( nn ) 6 ;# number o f m o b i l e n o d e s
set v a l ( rp ) AODV ;# r o u t i n g p r o t o c o l
set v a l ( x ) 500 ;# i n m e t r e s
set v a l ( y ) 500 ;# i n m e t r e s
#Adhoc OnDemand D i s t a n c e Vector

#c r e a t i o n o f S i m u l a t o r
set ns [ new S i m u l a t o r ]

#c r e a t i o n o f Trace and n a m f i l e
set t r a c e f i l e [ open w i r e l e s s . t r w ]
$ns t r a c e − a l l $ t r a c e f i l e

#C r e a t i o n o f Network Animation f i l e
set n a m f i l e [ open w i r e l e s s . n a m w ]
$ns n a m t r a c e − a l l − w i r e l e s s $ n a m f i l e $ v a l ( x ) $ v a l ( y )

#c r e a t e t o p o g r a p h y
set topo [ new Topography ]
$topo l o a d f l a t g r i d $ v a l ( x ) $ v a l ( y )

#GOD C r e a t i o n − General O p e r a t i o n s D i r e c t o r
create−god $ v a l ( nn )

set c h a n n e l 1 [ new $ v a l ( chan ) ]


set c h a n n e l 2 [ new $ v a l ( chan ) ]
set c h a n n e l 3 [ new $ v a l ( chan ) ]

#c o n f i g u r e t h e node
$ns node−config −adhocRouting $ v a l ( rp ) \
−llType $ v a l ( l l ) \
−macType $ v a l ( mac ) \
−ifqType $ v a l ( i f q ) \
−ifqLen $ v a l ( i f q l e n ) \
−antType $ v a l ( ant ) \
−propType $ v a l ( prop ) \
−phyType $ v a l ( n e t i f ) \
− t o p o I n s t a n c e $topo \

5
−agentTrace ON \
−macTrace ON \
−routerTrace ON \
−movementTrace ON \
−channel $ c h a n n e l 1

set n0 [ $ns node ]


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

$n0 random−motion 0
$n1 random−motion 0
$n2 random−motion 0
$n3 random−motion 0
$n4 random−motion 0
$n5 random−motion 0

$ns initial node pos $n0 20


$ns initial node pos $n1 20
$ns initial node pos $n2 20
$ns initial node pos $n3 20
$ns initial node pos $n4 20
$ns initial node pos $n5 50

#i n i t i a l c o o r d i n a t e s o f t h e nodes
$n0 set X 10 . 0
$n0 set Y 20 . 0
$n0 set Z 0 . 0

$n1 set X 210 . 0


$n1 set Y 230 . 0
$n1 set Z 0 .0

$n2 set X 100 . 0


$n2 set Y 200 . 0
$n2 set Z 0 .0

$n3 set X 150 . 0


$n3 set Y 330 . 0
$n3 set Z 0 .0

$n4 set X 430 . 0


$n4 set Y 320 . 0

6
$n4 set Z 0 .0

$n5 set X 270 . 0


$n5 set Y 120 . 0
$n5 set Z 0 . 0
#Dont mention any v a l u e s a bo v e than 500 b e c a u s e i n t h i s e x a m p l e , we use X and Y

#m o b i l i t y o f t h e nodes
#At what Time? Which node ? Where t o ? a t What Speed ?
$ns a t 1 . 0 ” $n0 s e t d e s t 490 . 0 340 . 0 35 . 0 ”
$ns a t 1 . 0 ” $n1 s e t d e s t 490 . 0 340 . 0 5 . 0 ”
$ns a t 1 . 0 ” $n2 s e t d e s t 330 . 0 100 . 0 10 . 0 ”
$ns a t 1 . 0 ” $n3 s e t d e s t 300 . 0 100 . 0 8 . 0 ”
$ns a t 1 . 0 ” $n4 s e t d e s t 300 . 0 130 . 0 5 . 0 ”
$ns a t 1 . 0 ” $n5 s e t d e s t 190 . 0 440 . 0 15 . 0 ”
#t h e nodes can move any number o f t i m e s a t any l o c a t i o n d u r i n g t h e s i m u l a t i o n ( r
$ns a t 20 . 0 ” $n5 s e t d e s t 100 . 0 200 . 0 30 . 0 ”

#c r e a t i o n o f a g e n t s
set t c p [ new Agent /TCP]
set s i n k [ new Agent /TCPSink ]
$ns attach−agent $n0 $ t c p
$ns attach−agent $n5 $ s i n k
$ns c o n n e c t $ t c p $ s i n k
set f t p [ new A p p l i c a t i o n /FTP]
$ f t p attach−agent $ t c p
$ns a t 1 . 0 ” $ f t p s t a r t ”

set udp [ new Agent /UDP]


set n u l l [ new Agent / N u l l ]
$ns attach−agent $n2 $udp
$ns attach−agent $n3 $ n u l l
$ns c o n n e c t $udp $ n u l l
set c b r [ new A p p l i c a t i o n / T r a f f i c /CBR]
$ c b r attach−agent $udp
$ns a t 1 . 0 ” $ c b r s t a r t ”

$ns a t 30 . 0 ” f i n i s h ”

proc f i n i s h {} {
global ns t r a c e f i l e n a m f i l e
$ns f l u s h − t r a c e
close $ t r a c e f i l e
close $namfile
exit 0
}

7
puts ” S t a r t i n g S i m u l a t i o n ”
$ns run

Figure 2: Wireless Network

1.4 Result
Installed NS-2 and implemented wired and wireless network simulation on NS-2
compiled on gcc 4.4 and executed on Debian 4.9 Kernel 4.9 and outputs were
verified.

You might also like