Ns 2
Ns 2
NETWORKS LABORATORY
INTRODUCTION
• Introduction to NS2 Simulator
• Network Simulator (Version 2), widely known as NS2, is simply an
event- driven simulation tool that has proved useful in studying the
dynamic nature of communication networks. Simulation of wired as
well as wireless network functions and protocols (e.g., routing
algorithms, TCP, UDP) can be done using NS2. In general, NS2
provides users with a way of specifying such network protocols and
simulating their corresponding behaviours
NS2- Architecture:
LANGUAGES
• NS2 consists of two key languages: C++ and Object-oriented Tool
Command Language (OTcl). While the CCC defines the internal
mechanism (i.e., a backend) of the simulation, the OTcl sets up
simulation by assembling and configuring the objects as well as
scheduling discrete events (i.e., a frontend).
• Operarational Procedure:
• • NS2 use Tcl language for creating simulation scenario file (for
example, ns1.tcl).
• • ns1.tcl :
• 1. It is the name of a tcl script file which defines the simulation
scenario.
• 2. Network topology, transmission time, using protocol etc... are
defined in scenario file.
• • If we execute this scenario file, the simulation results will be ns1.tr
and ns1.nam file.
• ns1.tr :
• 1. all the information about communication is written in this file.
• 2. This file is called as Trace file.
• • ns1.nam :
• 1. Used to visualize the simulation.
• 2. This file can be execute by Nam, an animation software.
• Steps to create a wired network scenario file:
• 1. Declare Simulator
• 2. Create output files (tracing and animation file )
• 3. Setting Node
• 4. Setting Link between the nodes
• 5. Setting Agent
• 6. Setting Application
• 7. Setting time schedules for Simulation
• 8. Declare finish
• Declare Simulator :
• 1. S should be capital letter in Simulator.
• • set ns [new Simulator]
• 1. Simulator is the name of the c++ class. We have to call this c++
class from the precompiled library.
• 2. new is to create the instants of this simulator
• Create output files :
• • set tracefile [open ns1.tr w]
• To write a TCL Script to create two nodes and links between nodes
and perform data transfer between them.
• Apparatus:
• PC, Ubuntu Linux, NS-2 Simulator, NSG2.1 Script Generator.
• TCL Script:
• # This script is created by NSG2 beta1
• # <http://wushoupong.googlepages.com/nsg>
• #===================================
• # Simulation parameters setup #===================================
• set val(stop) 5.0 ;# time of simulation end #===================================
• # Initialization #===================================
• #Create a ns simulator set ns [new Simulator]
• #Open the NS trace file
• set tracefile [open nodes.tr w]
• $ns trace-all $tracefile
• #Open the NAM trace file
• set namfile [open nodes.nam w]
• $ns namtrace-all $namfile
• Nodes Definition #===================================
• #Create 2 nodes set n0 [$ns node] set n1 [$ns node]
• #===================================
• # Links Definition #===================================
• #Createlinks between nodes
• $ns duplex-link $n0 $n1 50.0Mb 10ms DropTail
• $ns queue-limit $n0 $n1 100
• #Give node position (for NAM)
• $ns duplex-link-op $n0 $n1 orient right
• #Define a 'finish' procedure proc finish {} {
• global ns tracefile namfile
• $ns flush-trace close $tracefile close $namfile
• exec nam nodes.nam & exit 0
•}
• $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
• $ns at $val(stop) "finish"
• $ns at $val(stop) "puts \"done\" ; $ns halt"
• $ns run
• Procedure:
• 1. Open a Network simulator by using NSG2.jar and create a New wired
scenario.
• 2. Place a required node on the worksheet.
• 3. Provide a link between (Simplex/Duplex) them.
• 4. Assign Agents for the Node to transmit the data from source node to the
Destination node.
• 5. Provide Parameters as trace, nam file to verify the analysis on packet
delivery.
• 6. Save the TCL Script in NS2.35 location to execute.
• 7. Run the network animator with specified time interval and List the
parameters like Throughput, Packet Delivery Ratio, Delay etc.
• Result: Hence, TCL Script to create two nodes and links between
nodes are designed, generated and executed successfully