SlideShare a Scribd company logo
Presentation on-
Working with Network
Simulator 2
Presented By-
Chanchal
Agarwal
Id-47043
M.Tech 1st year
Hello World - Interactive Mode
chanchal>ns
% set ns [new Simulator]
_o3
% $ns at 1 “puts “Hello World!””
1
% $ns at 1.5 “exit”
2
% $ns run
Hello World!
chanchal>
Hello World - Batch Mode
simple.tcl
set ns [new Simulator]
$ns at 1 “puts “Hello World!””
$ns at 1.5 “exit”
$ns run
chanchal> ns simple.tcl
Hello World!
chanchal>
Tcl and OTcl
 TCL- Tool Command Language and pronounced
as "Tickle“.
 TCL is a scripting programming language -
similar to PERL or Python.
 Very good for quick and dirty jobs
 Object TCL (OTCL) is an object oriented
extension of TCL. OTcl is written in C++.
 OTCL's library of objects can
be extended by adding object definitions in
the C++ source code
NS-2 Design
NS-2 Design
 To use NS-2, a user programs should be in the
OTcl script language.
 An OTcl script will do the following.
 Initiates an event scheduler.
 Sets up the network topology using the network
objects.
 Tells traffic sources when to start/stop
transmitting packets through the event
scheduler.
 Depending on the user’s purpose for an OTcl
simulation script, simulation results are stored as
trace files, which can be loaded for analysis by
an external application:
1. A NAM trace file (file.nam) for use with the
Network Animator Tool.
2. A Trace file (file.tr) for use with XGraph or
TraceGraph.
Flow of events for a Tcl file
Languages Used in NS-2
 Split-Language programming is used .
Scripting Language (Tcl - Tool Command
Language and pronounced ‘tickle’)
System Programming Language (C/C++)
 Ns is a Tcl interpreter to run Tcl Scripts .
 By using C++/OTcl, the network simulator is
completely Object-oriented.
 TclCL is the language used to provide a linkage
between C++ and OTcl.
OTcl and C++: The Duality
NAM
 NAM- Network Animator.
 Provides a visual interpretation of the network
created.
 Can be executed directly from a Tcl script.
 Presents information such as throughput,
number packets on each link.
 Provides a drag and drop interface for creating
topologies.
Working with NS2
Trace Files
 Syntax For Creating A Trace File
# Open the trace file
set nf [open out.tr w]
$ns trace-all $nf
# Define finish procedure
proc finish {} {
global ns nf
$ns flush-trace
close nf
exit 0 }
Creating Network
 Nodes
set n0 [$ns node]
set n1 [$ns node]
 The following creates 5 nodes, with handles n0-n4
for {set i 0} {$i<5} {incr i} {Set n($i) [$ns node] }
 To set the colour of a node, the following code is used.
$n0 color <colour>
where <colour> is black, red, blue, seaGreen.
n0 n1
Creating Network
 Links and queuing
$ns duplex-link $n0 $n1 <bandwidth> <delay>
<queue_type>
1. <bandwidth>: 1000b, 1kb, 0.001Mb, …
2. <delay>: 1ms, 0.001s, …
3. <queue_type>: DropTail, RED, CBQ, FQ,
SFQ, DRR.
n0 n1
Creating Connection – Agents
 UDP
1. set src [new Agent/UDP]
2. set rcv [new Agent/Null]
3. $ns attach-agent $n0 $src
4. $ns attach-agent $n1 $rcv
5. $ns connect $src $rcv
 TCP
1. set tcp [new Agent/TCP]
2. set tcpsink [new Agent/TCPSink]
3. $ns attach-agent $n0 $src
4. $ns attach-agent $n1 $rcv
5. $ns connect $tcp $tcpsink
Traffic Applications
 The OTcl code to implement a CBR traffic
source in a simulation is as follows:
1. set my_cbr [new Application/Traffic/CBR]
2. $my_cbr attach-agent $udp
3. $ns at <time> “$my_cbr start”
 Parameters:
1. start: starts sending packets according to
the configuration parameters
2. stop: stops sending packets
Traffic Applications
 Configuration parameters:
1. PacketSize_: constant size of packets
generated e.g. 48
2. rate_: sending rate e.g. 64kb
3. interval_: (optional) interval time between
packets e.g. 0.05
4. random_: Flag to introduce noise in the
departure times, default is off, 1 for on
5. maxpkts_: the maximum number of packets
to send e.g. 1000
Traffic Applications
 Two simulation applications exist to send traffic on top of a TCP object:
Application/FTP and Application/Telnet.
File Transfer Protocol (FTP- for simulating bulk data transfer)
1. set ftp [new Application/FTP]
2. $ftp attach-agent $tcp
3. $ns at <time> “$ftp start”
Parameters
1. attach-agent: attaches an Application/FTP agent to an agent
2. start: start the Application/FTP to send data .
3. stop: stop sending data.
4. produce n: where n is the counter of packets to be sent .
5. producemore n: where n is the new increased value of packets to be sent .
6. send n: similar to producemore, but sends n bytes instead of packets .
Traffic Applications
 Telnet
1. OTcl code for using Telnet in a simulation:
2. set telnet [new Application/Telnet]
3. $telnet attach-agent $tcp
Parameters
1. start; start producing packets.
2. stop: stop producing packets.
3. attach-agent: attaches a Telnet object to an
agent.
Example-2
#Create a simulator object
set ns [new Simulator]
#Open the namtrace 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-2
#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
#Call the finish procedure after 5 seconds of simulation
time
$ns at 5.0 "finish"
#Run the simulation
$ns run
Example-3
#Create a simulator object
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 0 Blue
#Open the namtrace 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-3
#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
#Give node position (for NAM)
$ns duplex-link-op $n0 $n1 orient right-down
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# 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
Example-3
#Create a Null agent (a traffic sink) and attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
THANK
YOU

More Related Content

PPTX
ALOHA Protocol (in detail)
PDF
Introduction MQTT in English
PPTX
MQTT IOT Protocol Introduction
PPTX
RANDOM ACCESS PROTOCOL IN COMMUNICATION
PDF
Introduction to ns2
PDF
MQTT - MQ Telemetry Transport for Message Queueing
PDF
CS6701 CRYPTOGRAPHY AND NETWORK SECURITY
ALOHA Protocol (in detail)
Introduction MQTT in English
MQTT IOT Protocol Introduction
RANDOM ACCESS PROTOCOL IN COMMUNICATION
Introduction to ns2
MQTT - MQ Telemetry Transport for Message Queueing
CS6701 CRYPTOGRAPHY AND NETWORK SECURITY

What's hot (20)

PDF
Socket programming using C
PPSX
Packet Tracer Tutorial # 1
PPTX
Art network
PPT
Ieee 802.11 wireless lan
PPTX
Wireshark
PDF
Prometheus Multi Tenancy
PPT
Network layer tanenbaum
PPSX
Mac protocols of adhoc network
PPT
02 protocol architecture
PDF
MQTT - A practical protocol for the Internet of Things
PPTX
Mqtt(Message queue telemetry protocol) presentation
PPT
tcp ip protocols.ppt
PPTX
Switches on Networking
PPTX
Congestion control, slow start, fast retransmit
PPT
Error Detection And Correction
PPTX
Software Defined Network (SDN)
PDF
IEEE 802.11 Architecture and Services
PPT
SSL basics and SSL packet analysis using wireshark
PPTX
The medium access sublayer
Socket programming using C
Packet Tracer Tutorial # 1
Art network
Ieee 802.11 wireless lan
Wireshark
Prometheus Multi Tenancy
Network layer tanenbaum
Mac protocols of adhoc network
02 protocol architecture
MQTT - A practical protocol for the Internet of Things
Mqtt(Message queue telemetry protocol) presentation
tcp ip protocols.ppt
Switches on Networking
Congestion control, slow start, fast retransmit
Error Detection And Correction
Software Defined Network (SDN)
IEEE 802.11 Architecture and Services
SSL basics and SSL packet analysis using wireshark
The medium access sublayer
Ad

Viewers also liked (20)

PDF
NS-2 Tutorial
PPT
Ns 2 Network Simulator An Introduction
PPTX
Protocol implementation on NS2
PPT
Simulation and Performance Analysis of AODV using NS-2.34
DOCX
Ns2programs
PPT
NS2: Binding C++ and OTcl variables
PPT
PDF
Use of NS-2 to Simulate MANET Routing Algorithms
KEY
PDF
Cour simulation ns2
PDF
IEEE NS2 & NS3 PROJECT TITLE 2015-16
PPTX
Malicious node detection in vanet
DOC
Study of aloha protocol using ns2 network java proram
PPT
NS2 Projects Source Code NS2 Projects 2013 Wireless Sensor Networks Cognitive...
DOCX
Routage dans les réseaux ad hoc
PPTX
Marketing
PPTX
Vanet - routage unicast et adressage
PPTX
Marketing
PDF
NS2 Projects for Final Year Students, MANET, VANET, Ad-Hoc Networks...
NS-2 Tutorial
Ns 2 Network Simulator An Introduction
Protocol implementation on NS2
Simulation and Performance Analysis of AODV using NS-2.34
Ns2programs
NS2: Binding C++ and OTcl variables
Use of NS-2 to Simulate MANET Routing Algorithms
Cour simulation ns2
IEEE NS2 & NS3 PROJECT TITLE 2015-16
Malicious node detection in vanet
Study of aloha protocol using ns2 network java proram
NS2 Projects Source Code NS2 Projects 2013 Wireless Sensor Networks Cognitive...
Routage dans les réseaux ad hoc
Marketing
Vanet - routage unicast et adressage
Marketing
NS2 Projects for Final Year Students, MANET, VANET, Ad-Hoc Networks...
Ad

Similar to Working with NS2 (20)

PDF
study-of-network-simulator.pdf
PPT
NS2-tutorial.ppt
PDF
Cs757 ns2-tutorial-exercise
PDF
NS2-tutorial.pdf
PPT
Network Simulator Tutorial
PDF
Ns2: OTCL - PArt II
PDF
Ns2pre
PPTX
NErwork Lab Simulation Introduction.pptx
PPT
Venkat ns2
PDF
cscn1819.pdf
DOCX
NS2 (1).docx
PPTX
Network Simulator overview and its working
PPTX
Ns2 ns3 training in mohali
PPT
Tut hemant ns2
PDF
Introduction to ns2
PPT
Ns fundamentals 1
PPT
Ns2
PDF
Ns tutorial
PPT
NS2 Overview - Network Simulator Tutorial
PPT
ns2-lecture.ppt
study-of-network-simulator.pdf
NS2-tutorial.ppt
Cs757 ns2-tutorial-exercise
NS2-tutorial.pdf
Network Simulator Tutorial
Ns2: OTCL - PArt II
Ns2pre
NErwork Lab Simulation Introduction.pptx
Venkat ns2
cscn1819.pdf
NS2 (1).docx
Network Simulator overview and its working
Ns2 ns3 training in mohali
Tut hemant ns2
Introduction to ns2
Ns fundamentals 1
Ns2
Ns tutorial
NS2 Overview - Network Simulator Tutorial
ns2-lecture.ppt

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Digital Logic Computer Design lecture notes
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
PPT on Performance Review to get promotions
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT
Project quality management in manufacturing
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Welding lecture in detail for understanding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Automation-in-Manufacturing-Chapter-Introduction.pdf
additive manufacturing of ss316l using mig welding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Digital Logic Computer Design lecture notes
Operating System & Kernel Study Guide-1 - converted.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT on Performance Review to get promotions
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Mechanical Engineering MATERIALS Selection
Internet of Things (IOT) - A guide to understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Project quality management in manufacturing
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Lecture Notes Electrical Wiring System Components
Welding lecture in detail for understanding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
R24 SURVEYING LAB MANUAL for civil enggi

Working with NS2

  • 1. Presentation on- Working with Network Simulator 2 Presented By- Chanchal Agarwal Id-47043 M.Tech 1st year
  • 2. Hello World - Interactive Mode chanchal>ns % set ns [new Simulator] _o3 % $ns at 1 “puts “Hello World!”” 1 % $ns at 1.5 “exit” 2 % $ns run Hello World! chanchal>
  • 3. Hello World - Batch Mode simple.tcl set ns [new Simulator] $ns at 1 “puts “Hello World!”” $ns at 1.5 “exit” $ns run chanchal> ns simple.tcl Hello World! chanchal>
  • 4. Tcl and OTcl  TCL- Tool Command Language and pronounced as "Tickle“.  TCL is a scripting programming language - similar to PERL or Python.  Very good for quick and dirty jobs  Object TCL (OTCL) is an object oriented extension of TCL. OTcl is written in C++.  OTCL's library of objects can be extended by adding object definitions in the C++ source code
  • 6. NS-2 Design  To use NS-2, a user programs should be in the OTcl script language.  An OTcl script will do the following.  Initiates an event scheduler.  Sets up the network topology using the network objects.  Tells traffic sources when to start/stop transmitting packets through the event scheduler.
  • 7.  Depending on the user’s purpose for an OTcl simulation script, simulation results are stored as trace files, which can be loaded for analysis by an external application: 1. A NAM trace file (file.nam) for use with the Network Animator Tool. 2. A Trace file (file.tr) for use with XGraph or TraceGraph.
  • 8. Flow of events for a Tcl file
  • 9. Languages Used in NS-2  Split-Language programming is used . Scripting Language (Tcl - Tool Command Language and pronounced ‘tickle’) System Programming Language (C/C++)  Ns is a Tcl interpreter to run Tcl Scripts .  By using C++/OTcl, the network simulator is completely Object-oriented.  TclCL is the language used to provide a linkage between C++ and OTcl.
  • 10. OTcl and C++: The Duality
  • 11. NAM  NAM- Network Animator.  Provides a visual interpretation of the network created.  Can be executed directly from a Tcl script.  Presents information such as throughput, number packets on each link.  Provides a drag and drop interface for creating topologies.
  • 13. Trace Files  Syntax For Creating A Trace File # Open the trace file set nf [open out.tr w] $ns trace-all $nf # Define finish procedure proc finish {} { global ns nf $ns flush-trace close nf exit 0 }
  • 14. Creating Network  Nodes set n0 [$ns node] set n1 [$ns node]  The following creates 5 nodes, with handles n0-n4 for {set i 0} {$i<5} {incr i} {Set n($i) [$ns node] }  To set the colour of a node, the following code is used. $n0 color <colour> where <colour> is black, red, blue, seaGreen. n0 n1
  • 15. Creating Network  Links and queuing $ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type> 1. <bandwidth>: 1000b, 1kb, 0.001Mb, … 2. <delay>: 1ms, 0.001s, … 3. <queue_type>: DropTail, RED, CBQ, FQ, SFQ, DRR. n0 n1
  • 16. Creating Connection – Agents  UDP 1. set src [new Agent/UDP] 2. set rcv [new Agent/Null] 3. $ns attach-agent $n0 $src 4. $ns attach-agent $n1 $rcv 5. $ns connect $src $rcv  TCP 1. set tcp [new Agent/TCP] 2. set tcpsink [new Agent/TCPSink] 3. $ns attach-agent $n0 $src 4. $ns attach-agent $n1 $rcv 5. $ns connect $tcp $tcpsink
  • 17. Traffic Applications  The OTcl code to implement a CBR traffic source in a simulation is as follows: 1. set my_cbr [new Application/Traffic/CBR] 2. $my_cbr attach-agent $udp 3. $ns at <time> “$my_cbr start”  Parameters: 1. start: starts sending packets according to the configuration parameters 2. stop: stops sending packets
  • 18. Traffic Applications  Configuration parameters: 1. PacketSize_: constant size of packets generated e.g. 48 2. rate_: sending rate e.g. 64kb 3. interval_: (optional) interval time between packets e.g. 0.05 4. random_: Flag to introduce noise in the departure times, default is off, 1 for on 5. maxpkts_: the maximum number of packets to send e.g. 1000
  • 19. Traffic Applications  Two simulation applications exist to send traffic on top of a TCP object: Application/FTP and Application/Telnet. File Transfer Protocol (FTP- for simulating bulk data transfer) 1. set ftp [new Application/FTP] 2. $ftp attach-agent $tcp 3. $ns at <time> “$ftp start” Parameters 1. attach-agent: attaches an Application/FTP agent to an agent 2. start: start the Application/FTP to send data . 3. stop: stop sending data. 4. produce n: where n is the counter of packets to be sent . 5. producemore n: where n is the new increased value of packets to be sent . 6. send n: similar to producemore, but sends n bytes instead of packets .
  • 20. Traffic Applications  Telnet 1. OTcl code for using Telnet in a simulation: 2. set telnet [new Application/Telnet] 3. $telnet attach-agent $tcp Parameters 1. start; start producing packets. 2. stop: stop producing packets. 3. attach-agent: attaches a Telnet object to an agent.
  • 21. Example-2 #Create a simulator object set ns [new Simulator] #Open the namtrace 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 }
  • 22. Example-2 #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 #Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish" #Run the simulation $ns run
  • 23. Example-3 #Create a simulator object set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 0 Blue #Open the namtrace 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 }
  • 24. Example-3 #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 #Give node position (for NAM) $ns duplex-link-op $n0 $n1 orient right-down #Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 # 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
  • 25. Example-3 #Create a Null agent (a traffic sink) and attach it to node n1 set null0 [new Agent/Null] $ns attach-agent $n1 $null0 #Connect the traffic source with the traffic sink $ns connect $udp0 $null0 #Schedule events for the CBR agent $ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop" #Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish" #Run the simulation $ns run