You are on page 1of 19

NS2

programming

languages
There are two type of languages used to write the

program for network simulation

Frontend

TCL/Otcl

Backend

C/C++

Backend

In Ns2, backend consist with C++ coding


All protocols are developed by using C++ coding

Frontend
In order to use ns2, we must first understand the

programming language Tcl


The goal

how to setup an ns2 simulation


how to go about collecting and plotting data from your
simulations.

Don't forget companies like Pixar, Motorola, IBM,

Sybase, Nortel, Raytheon.., uses TCL language.

TCL
Tcl is a string-based command language.
A command does something for you, like output a

string, compute a math expression, or display a


widget on the screen.
The basic syntax for a Tcl command is:
command arg1 arg2 arg3 ...

The command is either the name of a built-in

command or a Tcl procedure.


White space (i.e., spaces or tabs) is used to separate
the command name and its arguments.
A newline (i.e., the end of line character) or
semicolon is used to terminate a command.
Quotes( ) and braces({ }) can both be used to group
several words into a single unit.

Variables
The set command is used to assign a value to a

variable.
It takes two arguments:

name of the variable


the value

The puts command prints the output to the screen.

set X "This is a string"


set Y 1.24
puts $X
puts $Y

OPERATORS
- + !

minus, plus, logical NOT.

* / %

Multiply, divide, remainder.

<< >>

Left and right (bit) shift.

< > <= >=

Relational operators: less, greater, less than or equal, and greater


than or equal

eq ne in ni

compare two strings for equality (eq) or inequality (ne). and two
operators for checking if astring is contained in a list (in) or not (ni)

New command
OTcl allows you to define your own commands,

which you can think of as procedures or functions in


other languages.
To define a new command, you must use a command
called proc, which takes three arguments.

The name of the new command,


The list of arguments,
The last is the body of the command (what your command will
do).

Example
% proc triple arg1 { return [expr $arg1 * 3] }
% triple 9
27
% set t [triple 9]
27
% puts $t
27

Example

proc justdoit {a {b 1} {c -1}} {


}
o justdoit 10
o

set a to 10, and leave b set to its default 1, and c at -1.

o justdoit 10 20
o set b to 20, and leave C to its default

Control structures
Tcl comes with a number of built in commands for

control structures.

if statements
different kinds of loops.

Ex(if)
set x 5
i f { $x == 1 } {
puts TRue
} el se {
puts not True
}

Ex(else if )
i f { $x == 1 } {
puts f i r s t
} e l s e i f { $x == 2 } {
puts second
} el se {
puts thi r d
}

while
set value 1
set fact 2
while { $ fact <= 5 } {
set value [ expr $value +$ fact ]
incr fact
}
puts $value

Ex(for)
set sum 0
for { set i 0 } { $ i < 10 } { incr i } {
set sum [ expr $sum + $ i ]
}
puts $sum

File Access
Tcl provides several methods to read from and write

to files on disk.

Gets command

puts command

Ns2 objects

You might also like