Tcl Script for ns Simulation: A Simple Topology with Two Nodes and One Link, Study notes of Computer Systems Networking and Telecommunications

A tcl script for ns simulation that sets up a simple topology with two nodes connected by a link. It explains how to create nodes, connect them with a link, and send data between them using udp agents and cbr traffic source. The document also suggests experiments and references the ns manual page for more information.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-jan
koofers-user-jan 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
IV. The first Tcl script
[Previous section] [Next section] [Back to the index]
In this section, you are going to develop a Tcl script for ns which simulates a simple topology. You are
going to learn how to set up nodes and links, how to send data from one node to another, how to monitor
a queue and how to start nam from your simulation script to visualize your simulation.
IV.1. How to start
Now we are going to write a 'template' that you can use for all of the first Tcl scripts. You can write your
Tcl scripts in any text editor like joe or emacs. I suggest that you call this first example 'example1.tcl'.
First of all, you need to create a simulator object. This is done with the command
Now we open a file for writing that is going to be used for the nam trace data.
The first line opens the file 'out.nam' for writing and gives it the file handle 'nf'. In the second line we
tell the simulator object that we created above to write all simulation data that is going to be relevant for
nam into this file.
The next step is to add a 'finish' procedure that closes the trace file and starts nam.
You don't really have to understand all of the above code yet. It will get clearer to you once you see
what the code does.
The next line tells the simulator object to execute the 'finish' procedure after 5.0 seconds of simulation
time.
You probably understand what this line does just by looking at it. ns provides you with a very simple
way to schedule events with the 'at' command.
The last line finally starts the simulation.
You can actually save the file now and try to run it with 'ns example1.tcl'. You are going to get an error
message like 'nam: empty trace file out.nam' though, because until now we haven't defined any objects
(nodes, links, etc.) or events. We are going to define the objects in
section 2
and the events in
section 3
.
set ns [new Simulator]
set nf [open out.nam w]
$ns namtrace-all $nf
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
$ns at 5.0 "finish"
$ns run
Page
1
4
Marc Greis' Tutorial for the UCB/LBNL/VINT Network Simulator "ns"
11/16/2003
http://www.isi.edu/nsnam/ns/tutorial/nsscript1.html
pf3
pf4

Partial preview of the text

Download Tcl Script for ns Simulation: A Simple Topology with Two Nodes and One Link and more Study notes Computer Systems Networking and Telecommunications in PDF only on Docsity!

IV. The first Tcl script

[Previous section] [Next section] [Back to the index]

In this section, you are going to develop a Tcl script for ns which simulates a simple topology. You are

going to learn how to set up nodes and links, how to send data from one node to another, how to monitor

a queue and how to start nam from your simulation script to visualize your simulation.

IV.1. How to start

Now we are going to write a 'template' that you can use for all of the first Tcl scripts. You can write your

Tcl scripts in any text editor like joe or emacs. I suggest that you call this first example 'example1.tcl'.

First of all, you need to create a simulator object. This is done with the command

Now we open a file for writing that is going to be used for the nam trace data.

The first line opens the file 'out.nam' for writing and gives it the file handle 'nf'. In the second line we

tell the simulator object that we created above to write all simulation data that is going to be relevant for

nam into this file.

The next step is to add a 'finish' procedure that closes the trace file and starts nam.

You don't really have to understand all of the above code yet. It will get clearer to you once you see

what the code does.

The next line tells the simulator object to execute the 'finish' procedure after 5.0 seconds of simulation

time.

You probably understand what this line does just by looking at it. ns provides you with a very simple

way to schedule events with the 'at' command.

The last line finally starts the simulation.

You can actually save the file now and try to run it with 'ns example1.tcl'. You are going to get an error

message like 'nam: empty trace file out.nam' though, because until now we haven't defined any objects

(nodes, links, etc.) or events. We are going to define the objects in section 2 and the events in section 3.

set ns [new Simulator]

set nf [open out.nam w] $ns namtrace-all $nf

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

$ns at 5.0 "finish"

$ns run

You will have to use the code from this section as starting point in the other sections. You can download

it here.

IV.2. Two nodes, one link

In this section we are going to define a very simple topology with two nodes that are connected by a

link. The following two lines define the two nodes. (Note: You have to insert the code in this section

before the line '$ns run', or even better, before the line '$ns at 5.0 "finish"').

A new node object is created with the command '$ns node'. The above code creates two nodes and

assigns them to the handles 'n0' and 'n1'.

The next line connects the two nodes.

This line tells the simulator object to connect the nodes n0 and n1 with a duplex link with the bandwidth

1Megabit, a delay of 10ms and a DropTail queue.

Now you can save your file and start the script with 'ns example1.tcl'. nam will be started automatically

and you should see an output that resembles the picture below.

You can download the complete example here if it doesn't work for you and you think you might have

made a mistake.

IV.3 Sending data

Of course, this example isn't very satisfying yet, since you can only look at the topology, but nothing

actually happens, so the next step is to send some data from node n0 to node n1. In ns, data is always

being sent from one 'agent' to another. So the next step is to create an agent object that sends data from

node n0, and another agent object that receives the data on node n1.

set n0 [$ns node] set n1 [$ns node]

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

#Create a UDP agent and attach it to node n set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp

Create a CBR traffic source and attach it to udp

[Previous section] [Next section] [Back to the index]

ns-users

[email protected]