Network Simulator NS2-Beginner’s Guide


What is NS2?


Network simulator is a piece of software or hardware that predicates the performance of a network without a real network being there.NS2 is a vital simulation tool for networks. It supports a number of algorithms for routing and queuing. It can set up packet traffic related to internet and measure a variety of parameters. NS2 is very helpful because it is very costly to verify viability of new algorithms, test architectures, check topologies etc. network simulator is a name for series of discrete event network simulator. Simulators are used in the simulation of routing protocols, and are heavily used in ad-hoc networking research, and support popular network protocols, offering simulation results for wired and wireless networks alike. Ns2 program is helpful in many ways.

Features of NS2

NS2 can be employed in most unix systems and windows. Most of the NS2 code is in C++. It uses TCL as its scripting language, Otcl adds object orientation to TCL.NS(version 2) is an object oriented, discrete event driven network simulator that is freely distributed and open source.

  • Traffic Models: CBR, VBR, Web etc
  • Protocols: TCP, UDP, HTTP, Routing algorithms,MAC etc
  • Error Models: Uniform, bursty etc
  • Misc: Radio propagation, Mobility models , Energy Models
  • Topology Generation tools
  • Visualization tools (NAM), Tracing

Structure of NS

● NS is an object oriented discrete event simulator

– Simulator maintains list of events and executes one event after another

– Single thread of control: no locking or race conditions

● Back end is C++ event scheduler

– Protocols mostly

– Fast to run, more control

  • Front end is OTCL

Creating scenarios, extensions to C++ protocols

fast to write and change


It can be employed in most unix systems(FreeBSD, Linux, Solaris) and Windows.

Source code

Most of NS2 code is in C++

Scripting language

It uses TCL as its scripting language OTcl adds object orientation to TCL.

Protocols implemented in NS2

Transport layer(Traffic Agent) – TCP, UDP

Network layer(Routing agent)

Interface queue – FIFO queue, Drop Tail queue, Priority queue

Logic link contol layer – IEEE 802.2, AR

How to use NS2

Design Simulation – Determine simulation scenario

Build ns-2 script using tcl.

Run simulation

Simulation with NS2

Ø  Define objects of simulation.

Ø  Connect the objects to each other

Ø  Start the source applications. Packets are then created and are transmitted through network.

Ø  Exit the simulator after a certain fixed time.

NS programming Structure

● Create the event scheduler

● Turn on tracing

● Create network topology

● Create transport connections

● Generate traffic

● Insert errors

Creating Event Scheduler

● Create event scheduler: set ns [new simulator]

● Schedule an event: $ns at <time> <event>

– event is any legitimate ns/tcl function

$ns at 5.0 “finish”

proc finish {} {

global ns nf

close $nf

exec nam out.nam &

exit 0


● Start Scheduler

$ns run



● All packet trace

$ns traceall[open w]

<event> <time> <from> <to> <pkt> <size>

<flowid> <src> <dst> <seqno> <aseqno>

+  0.51  0  1 cbr  500 —–  0  0.0  1.0   0   2

_   0.51  0  1 cbr  500 —– 0   0.0  1.0  0   2

R  0.514 0  1 cbr  500 —–0   0.0   1.0  0   0

● Variable trace

set par [open output/ w]

$tcp attach $par

$tcp trace cwnd_

$tcp trace maxseq_

$tcp trace rtt_

Tracing and Animation

● Network Animator

set nf [open out.nam w]

$ns namtraceall


proc finish {} {

global ns nf

close $nf

exec nam out.nam &

exit 0


Creating topology

● Two nodes connected by a link

● Creating nodes

set n0 [$ns node]

set n1 [$ns node]

● Creating link between nodes

$ns <link_type> $n0 $n1 <bandwidth> <delay><queue-type>

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

Data Sending

Create UDP agent

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

Create CBR traffic source for feeding into UDP agent

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005

$cbr0 attach-agent$udp0

Create traffic sink

set null0 [new Agent/Null]

$ns attach-agent$n1 $null0

● Connect two agents

$ns connect $udp0 $null0

● Start and stop of data

$ns at 0.5 “$cbr0 start”

$ns at 4.5 “$cbr0 stop”



Traffic on top of TCP


set ftp [new Application/FTP]

$ftp attach-agent$tcp0

● Telnet

set telnet [new Application/Telnet]

$telnet attach-agent$tcp0



STEP 1: Start

STEP 2: Create the simulator object ns for designing the given simulation

STEP 3: Open the trace file and nam file in the write mode

STEP 4: Create the nodes of the simulation using the ‘set’ command

STEP 5: Create links to the appropriate nodes using $ns duplex-link command

STEP 6: Set the orientation for the nodes in the simulation using ‘orient’ command

STEP 7: Create TCP agent for the nodes and attach these agents to the nodes

STEP 8: The traffic generator used is FTP for both node0 and node1

STEP 9: Configure node1 as the sink and attach it

STEP10: Connect node0 and node1 using ‘connect’ command

STEP 11: Setting color  for the nodes

STEP 12: Schedule the events for FTP agent 10 sec

STEP 13: Schedule  the simulation for 5 minutes