You are on page 1of 30

NS-2 PROGRAM BASICS

BY
Dr.CH. Sita kumari
Network Simulator(NS-2)
 A package of tools that simulates behaviour of
networks

 Create Network Topologies – Log events that


happen under any load –

 Analyse events to understand the network


behaviour

 Learn fundamentals of evaluating network


performance via simulation
Why Simulation?
 Real-system not available, is complex/costly or
dangerous (eg: space simulations, flight
simulations)

 Quickly evaluate design alternatives (eg: different


system configurations)

 Evaluate complex functions for which closed form


formulas or numerical techniques not available
Creating Topologies
 Topologies are creating either Wired
simulation and wireless simulation in NS-2.
Here, we are going to explain about how to
work with wired simulation.
Wired Simulation in NS2:[Steps to create simulation script in
NS2]

1)Create a simulator object

2) Create trace object t and nam objects

3) Define a finish procedure

4) Create nodes either random or sequential

5) Connect node with another node with


duplex/simplex links
NS-2 program steps cont…
 6) Create agents to carry data either UDP(User
Datagram Protocol) flow or TCP flow

 7) Create an application transfer from one node to


another node either CBR or FTP

 8) Attach the applications to the agents and


scheduling the events

 9)call the finish procedure

 10) Start simulation


NS-2 program steps cont…
 Step1:To create simulator class object:

# create simulator object


Set   ns  [new   Simulator]
NS-2 program steps cont…
Step2: To open the name file and data file to store
the simulation results and data those files.
# open data file to store result and Tracing the
simulation result(Tracefile)
set  f1  [ open  output.out  w]
$ns  trace – all  $f1
 
#Tracing NAM
Set  nf  [open  out. nam  w]
$ns   namtrace – all  $nf
 
NS-2 program steps cont…
Step 3: Define a finish procedure
# Finish procedure to perform operation at the
end of simulation
proc finish { } {

# Mapping of global variable into local variable


global ns f1 nf

# Flush all buffers


$ns flush-trace
 
NS-2 program steps cont…
#close trace file and nam file objects
close $f1
close $nf
 
#Execute nam process in background
exec nam out.nam &

#Terminate current process


exit
NS-2 program steps cont…
Step4:To create Nodes
Case 1:For sequential node creation
set  nodenumber1 [$ns   node]
set  nodenumber2 [$ns   node]
...
set  nodenumbern [$ns   node]

example:
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
$n1 color Red
$n1 shape box
NS-2 program steps cont…
 case 2: If you want to create more number of
Nodes, use looping structure:

 # Create 5 Nodes
for {set  i  0} {  $i < 5 } { incr  i}
{
set  n($i) [$ns   node]
}
NS-2 program steps cont…

Step5: Connect node with another node with


duplex/simplex links
Syntax: $ns simplex-link/duplex-link [node-instance1]
[node-instance2] bandwidth delay Q-Type

# Connect nodes for two-way communication


$ns duplex-link $n0 $n1 2Mb 5ms DropTail
$ns simplex-link $n0 $n1 3Mb 2ms DropTail
$ns simplex-link $n1 $n0 2Mb 5ms DropTail
Setting orientation of links in NAM

Syntax : $ns simplex-link-op/duplex-link-op [node-


instance1] [node-instance2] orient [orientation]

$ns duplex-link-op $n0 $n1 orient left-up 


$ns duplex-link-op $n0 $n1 orient left-down
Different node positions
NS-2 program steps cont…
Step 6:To create Agents[TCP or UDP]

Syntax:$ [simulator-instance] attach-agent


[node-instance] [agent-instance]

# Create a object of UDP agent and assign it to


variable agnt1

$ set agnt1 [new Agent/UDP]


NS-2 program steps cont…
# To Create TCP agent
set  tcp1 [ new  Agent/TCP ]
$ns  attach – agent  $n1  $tcp1
 
# To Create TCP receiver
set tcp2 [ new  Agent/TCPSink ]
$ns  attach – agent  $n2  $tcp2
NS-2 program steps cont…
 Step 7: To create a application objects

$set cbr [new Application/Traffic/CBR]

$set ftp [new Application/FTP]

$set tel [new Application/Telnet]


NS-2 program steps cont…
Step 8:To attach FTP Application with Agent and
schedule the Event

# Create a FTP object


$set ftp0 [new Application/FTP] 

# Attach ftp application with agent tcp0


$ftp attach-agent $tcp0
NS-2 program steps cont…
# Schedule events

$ns at 0.2 "$ftp start" // FTP start at 0.2 ms


$ns at 2.0 "$ftp stop“ //FTP stop at 2.0 ms

 Step 7:calling finish function :

 $ns  at  2.3   “finish”

 Step 8. Start Simulation


 $ns run
A simple Example – Creating the
topology
Example cont…

#create a new 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
}
Example cont…
#create two nodes
set n0 [$ns node]
set n1 [$ns node]

#create a duplex link between the nodes


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

#create a udp agent and attach it to node n0


set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
Example cont…
#Create a CBR traffic source and attach it to
udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

#create a Null agent(a traffic sink) and attach it


to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
Example cont…
#Connect the traffic source to the sink
$ns connect $udp0 $null0

#Schedule events for CBR traffic


$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"

#call the finish procedure after 5 secs of simulated time


$ns at 5.0 "finish"

#run the simulation


$ns run
Simulation Environment
Trace file fields
Trace file fields cont…
References

 https
://www.youtube.com/watch?v=ZAjYDHpVplg

 https
://www.isi.edu/nsnam/ns/tutorial/nsscript1.
html

You might also like