NS2 Programming Tutorial offers you a compact guidance on NS2 basics to equip you with the complete knowledge of NS2. Our NS2 Programming Tutorial service has started with the collaboration of top experts and NS2 developers from all over the world. We have started our service, with a motive to help the beginners with the basics of NS2 programming. Once you are done with NS2 programming, we will offer you our complete guidance to develop your own project, assignment or research work in NS2, to make your fundamentals strong in NS2.You can be a beginner in NS2, but once you commit with us, we can make you an expert of NS2. As an initial step towards our guidance, we have provided few basic programming concepts in NS2 below. Approach us for further guidance; and explore the field of NS2 simulation.


    NS2 programming mainly deals two major programming paradigm i.e. C++[programming language] and Tool Command language[TCL- Scripting language]. To be more precise, NS2 is implemented in OTCL [Object oriented extension of TCL]. Let’s have a glance over the basics of TCL and OTCL.


  • Used to write simulation script in NS2.
  • It is an interpreted language which has each instruction as a command
  • To declare variable in TCL:

“Set” command is used to create a variable and assign value to it.


set   < variable name >   < variable value >

For ex. Set a 10


Here, we have declared a variable “a” with value 10.

To write the value of the variable, use the command,


Set a;

To unset the variable use,

Unset a;

To use the variable value:

  • Value of any variable is used by using the symbol $. For ex, if we want to assign a value of a to another variable b, use the command:

set b $a


To perform the basic operation:

  • Here, we make use of expr command which takes entire expression as arguments.


Sample Example:

         Set  x 20

set  y 30

expr  $x + $y// output will be 50

IF-else loop in TCL:






If – body


// Another syntax

if  [ expr  condition]


If – body


Looping in NS2:

  • Similar to loop statement as in C. Only difference is the condition specification.

  //while Loop








//for loop

For  {  initialization }  {  condition }  { increment/decrement }


Loop body


For Arrays in TCL:

  •  TCL supports named array i.e. string is given as named variable.

For  ex.

set x(0)   10

set x(1)    30

puts   $  x(0)

To define a procedure in TCL:

  • Used to combine multiple commands into a new command

Proc   < procedure name >

{  }


procedure body



File Handling in TCL:

Before performing any operation on a file, it must be first opened. Various modes in which it is opened are:

  • Read only mode[r]
  • Append Mode[a]
  • Read and Write[r+]
  • Write only mode[w]
  • Write and Read[w+]


Open   <file name>  <acc_type> // for opening the file

open  filename.txt  r[specify mode]


  • Object Oriented extension of TCL(Tool Command language)
  • Basics of OTCL:

Define a class in OTCL:

  • Classes can be taken as defined data types where as objects are considered as variables of that data types.


Class < class name >To define a class data member:·        Defined using the “set” command with object name

< object name >  set  < attribute name >  < attribute value >

To define methods in class:[Use of instproc command]:

 < class  name >  instproc  < function name >

{  arguments  }


Function body


To define access class attribute inside the method:

$ self   instvar   < variable name >   < variable name >

To define Constructor and Destructor:

Define Constructor:

  • Used to assign default values to data members. Let’s take an example program to understand it better.

Class Acc

Acc instproc init {  }


$self instvar name  balance  type

$self  set  balance 00

$self  set type  “current”


Acc xx

puts [xx set balance]

puts [xx set type]//output xx 00 balance

To define the Destructor:

  • Does not have the same name as class name
  • Used to remove the object from the interpreter and releases its memory.

To define Inheritance in OTCL:

  • Inheritance is used to create new class from the existing class,
  • Existing class is known as super class while the inherited class is known as sub class.
  • This concepts is based on OOPs concept

General Syntax:

Class   < sub classname >  super class  < super classname >



     We have provided an overall basic about OTCL and TCL for students to learn about its basic syntax and programming. Another, major language to be focused in NS2 is C++. Students can refer our other articles, to get more idea about C++ in NS2. For further guidance and tutoring on NS2, approach us through our online tutoring service. You can also approach us, if you feel to learn NS2 programming along with your project or assignment accomplishment.