learnbywatch
Play Video

Writing the first Tcl script in Network simulator – 2

After watching this video you will be able to:

No data was found

Tcl script is for scenario generation. Where to place nodes or what protocol do they follow and values of different parameters are defined in Tcl script. These are connected to C++ source codes through a ‘Tcl hook’. 

Let us take a look into first Tcl script

First, a simulator object needs to be created. Write the following command in any text editor.

set ns [new Simulator]

then two files are opened for writing data of trace file and nam file. write the following commands.

set nf [open out.nam w]

$ns namtrace-all $nf

set nf1 [open out.tr w]

$ns trace-all $nf1

first two lines open a file named out.nam in write mode and last two lines open a file named out.tr in write mode. All the events occurring during the simulation will be written in these two files. Trace file will be used for obtaining results through awk script and nam file will be used to see simulation animation.

Now we have to define nodes. let us define only two nodes in this example. Use the following commands

set n0 [$ns node]

set n1 [$ns node]

these two lines will create two variables named n0 and n1 both representing a node. Numbering of nodes will be done according to 0, 1, 2, ….. regardless of variable name given to it. In above two lines id of node n0 will be taken as 0 and id of node n1 will be taken as 1.

Now a link needs to be created between these two nodes for data transmission. use the following command.

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

it will create a duplex link between node n0 and n1 with a bandwidth of 1Mbits, delay for transmission in the link is 10msec and it will use droptail queue.

Now, nodes are placed and a link between those is established. but what is to be transmitted and who will transmit and who will receive? These are not defined yet.

Now we will have to assign Transport layer and application layer protocols to the source. first, create to create an UDP agent write the following command

set udp0 [new Agent/UDP]

now this udp0 agent should be attached to any of the nodes. Let us take node n0 as source in this scenario. to attach udp0 to n0, write the following commands.

$ns attach-agent $n0 $udp0

To assign an application layer protocol to any of the nodes, first write the following commands.

set cbr0 [new Application/Traffic/CBR]

it will create a CBR (constant bit rate) application. To change the properties of this application give the following commands

$cbr0 set packetSize_ 512

$cbr0 set interval_ 0.05

cbr0 will generate packets after each 0.05 seconds and each packet will be of 512 bytes.

To attach cbr0 to udp0 write the following commands:

$cbr0 attach-agent $udp0

now, cbr0 and udp0 are connected to each other. udp0 and n0 were already connected to each other. Hence,  cbr0, udp0 and n0 are all connected to each other. This means that node n0 will use application layer protocol CBR and Transport layer protocol UDP. Hence source node is ready.

There should be a receiver too. For this, a Null agent is attached to any of the node. Since we have only n1 left, it will be the receiver. write the following commands to create a Null agent.

set null0 [new Agent/Null]

Now attach this null0 to node n1

$ns attach-agent $n1 $null0

Now receiver is also created. But we have not given any command so that n0 should transmit to n1. for this write the following command

$ns connect $udp0 $null0

now write the following lines to start cbr0 and stop it at a particular time of simulation.

$ns at 1 “$cbr0 start”

$ns at 4 “$cbr0 stop”

it will start cbr0 at 1 second and stop at 4th second of simulation.

Now there is a function called finish (you can give it any name) we need to run at the end of simulation. write this function as

proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}

now we need to call this function

$ns at 5.0 “finish”

it will finish the simulation at 5th second of simulation, trace file will be closed and nam file will be executed.

finally start the simulation

$ns run

now save the file as anyname.tcl and go to the path of that file through terminal and give the following command

ns anyname.tcl

after pressing enter, a NAM window will be opened. click the play button and see how packets are transmitted from node 0 to node 1.

now go to that folder and open trace file out.tr. You will see a lot of data written there. what that data say and how to read that data to get your output through AWK script. we will see in next blogs.

What is your goal?

No data was found

2 thoughts on “Writing the first Tcl script in Network simulator – 2”

Comments are closed.

Micro Courses using this Video

No data was found
Scroll to Top
Scroll to Top