Simple TCL Program in NS2 is the best way to learn about TCL programs in NS2. TCL[Tool command language] is one of the major language for implementing Ns2. To be more specific, NS2 is implemented in C++ and OTCL[Object oriented extension of TCL]. C++ is used to program the internal mechanisms while OTCL is used for configuration.


A configuration file in NS2 is an OTCL file called “TCL Simulation script”. It also contains information about what we would like to simulate like node creation, also topology creation, setting up link etc. A TCL file is also an input configuration file for C++ file. To run the TCL file, use the command

                                                    ns filename.tcl

  Here the ns is also an executable program obtained by compiling the entire NS2 code using “make” utility and the .tcl file is an input configuration file of executable ns file. Now , let’s have also a glance over basic commands also using TCL along with a simple TCL program in NS2, also for students to learn about TCL completely.

What is TCL:
  • TCL[Tool command language] is also an scripting programming language
  • NS2 also uses OTCL[Object oriented extension of TCL]
  • OTCL is also written in C++ [OTCL=OOPS concepts+ TCL]
  • OTCL-configures the system while C++ is also used to implement the code that is frequently executed

Learn to program using TCL:

Define a Simulator:
  • Create a simulator also using the following TCL code,

              Set nssim [new simulator]

Define the topology:
  • Define the size of the area in which simulation should take place
  • Next, we need to define the position of nodes in the network based on the size.

settopo       [new Topography]

$topoload_flatgrid $val(x) $val(y)

Defining the Output trace files:
  • Used to record the events also which occurs during the simulation
  • Here events are also used to signify the node creation, data transfer etc.
  • Several trace files are available; we can also choose the trace file as per the result we are also looking for.
  • TCL Script for defining the output trace files as follows:

      Set tracef [open w]

     $ nssimtrace-all $tracef

Defining a General Operations Director[GOD]
  • GOD is used to store the global information about the network, nodes and state of environment.
  • Used to manage detailed operations like Movement patterns of mobile nodes. To create a god object, use the following command,

Create-god (1)

To Define AP/BS and Configure AP/BS
  • In Ns2, formation of complete network also includes the presence of Access points/Base stations.
  • It is necessary to initialize and also configure AP/BS in most of the network
  • Commands to create and also configure AP/BS using TCL

             To create AP/BS:                    

               Set ap_name [$nssimap]   

             To configure AP/BS:

$nssimap-config –optn_1 val_1\



    All the above mention TCL scripts are also used to design a network with all the required parameters and also characteristics of the network in NS2.  Next, we have provide a simple TCL script, which also explains you completely about how to program using TCL.

Simple TCL Program in NS2:

set ns [new Simulator]\”Defining Simulator”\

settracefd       [open w]\”Intializing trace file”\

settopo       [new Topography]\”Defining Topography”\

$topoload_flatgrid $val(x) $val(y)

set windowVsTime2 [open w]

setnamtrace      [open Smart_Grid_Network_Topology.nam w]

$ns trace-all $tracefd

$ ns namtrace-all-wireless $namtrace $val(x) $val(y)

create-god (1)\”Creating GOD”\

set god_ [God instance]

$ns at 0.5000 “$node_(0) setdest 100 400 0.15667896543”

$node_(0) set Z_ 0.0

$ node_(0) set Y_ 128

$node_(0) set X_ 142

$ns at 0.5000 “$node_(1) setdest 150 450 0.15667896543”

$node_(1) set Z_ 0.0

$ node_(1) set Y_ 128

$node_(1) set X_ 44

$ns at 0.5000 “$node_(2) setdest 140 400 0.15667896543”

$node_(2) set Z_ 0.0

$ node_(2) set Y_ 193

$node_(2) set X_ 144

$ns at 0.5000 “$node_(3) setdest 160 350 0.15667896543”

$node_(3) set Z_ 0.0

$ node_(3) set Y_ 199

$node_(3) set X_ 55

$ns at 0.5000 “$node_(4) setdest 120 270 0.15667896543”

$ node_(4) set Z_ 0.0

$node_(4) set Y_ 243

$ node_(4) set X_ 107

if { “$val(traffic)” == “tcp” } {


} else {



$ns duplex-link $node_(0) $node_(2) 2Mb 40ms RED

$ ns duplex-link $node_(2) $node_(4) 2Mb 40ms RED

$ns duplex-link $node_(4) $node_(3) 2Mb 40ms RED

$ ns duplex-link $node_(3) $node_(1) 2Mb 40ms RED

$ns duplex-link-op $node_(0) $node_(2) color “lavender”

$ ns duplex-link-op $node_(2) $node_(4) color “lavender”

$ns duplex-link-op $node_(4) $node_(3) color “lavender”

$ ns duplex-link-op $node_(3) $node_(1) color “lavender”

settcp [new Agent/TCP]

$tcp set class_ 2

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ ns attach-agent $n3 $sink

$ns connect $tcp $sink

$tcp set fid_ 1

$ ns at 0.1 “$cbr start”

$ns at 1.0 “$ftp start”

$ ns at 4.0 “$ftp stop”

$ns at 4.5 “$cbr stop”

$ ns at 4.5 “$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink”

$ns at 5.0 “finish”

puts “CBR packet size = [$cbr set packet_size_]”

puts”CBR interval = [$cbr set interval_]”

$ns run

     Hope you also would have learnt some basics about TCL. If you wish to have more information about TCL or feel to work on NS-2 also using TCL, approach our online tutoring service. Our experts will teach you completely about TCL in such a way that you will also able to code your own project in the future. We have focused mainly on TCL in this article, if you need any other information regarding NS2 programming, refer also our other articles. Once you are also done with NS2 programming basics, you can approach us for further project guidance and code support. We are also always ready to support you with all your needs and also requirements.


GROW WITH US……………………

SHINE WITH US,……………………………………….