Data Communications and Networks Lab File
Data Communications and Networks Lab File
LAB MANUAL
ON
R20INF31L2 : DATA COMMUNICATIONS AND NETWORKS
LAB
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.
proc finish {} {
global ns nf
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.
1/3
$ns run
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 the
events.
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'.
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.
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.
2/3
#Create a UDP agent and
attach it to node n0 set udp0
[new Agent/UDP]
These lines create a UDP agent and attach it to the node n0, then attach a CBR traffic generatot
to the UDP
agent. CBR stands for 'constant bit rate'. Line 7 and 8 should be self-explaining. ThepacketSize
is being set to 500 bytes and a packet will be sent every 0.005 seconds (i.e. 200 packets per
second). You can find the relevant parameters for each agent type In The next lines create a
Null agent which acts as traffic sink and attach it to node n1.
And now we have to tell the CBR agent when to send data and when to stop sending. Note: It's
probably best to put the following lines just before the line '$ns at 5.0 "finish"'.
Now you can save the file and start the simulation again. When you click on the 'play' button in
the nam
window, you will see that after 0.5 simulation seconds, node 0 starts sending data packets to
node 1. You might want to slow nam down then with the 'Step' slider.
3/3
I suggest that now you start some experiments with nam and the Tcl script. You can click on
any packet in the nam window to monitor it, and you can also click directly on the link to get
some graphs with statistics. I also suggest that you try to change the 'packetsize_' and
'interval_' parameters in the Tcl script to see what happens. You can download the full
example here.
Most of the information that I needed to be able to write this Tcl script was taken directly from
the example files in the 'tcl/ex/' directory, while I learned which CBR agent arguments
(packetSize_, interval_) I had to set from the ns manual page.
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
$ns run
4/3
1. Writing a TCL Script to create two nodes and links between nodes
$ns namtrace-all
$nf
#Define a 'finish' procedure
proc finish {}
{
global ns nf
$ns flush-trace
#Close the trace file
close
$nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
5/3
$ns at 5.0 "finish"
#Run the simulation
$ns run
OUT PUT
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
6/3
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns run
OUT PUT
7/3
$ns namtrace-all $nf
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exit 0
#Monitor the queue for the link between node 2 and node 3
$ns duplex-link-o
8/3
p $n2 $n3 queuePos 0.5
9/
#Schedule events for the CBR agents
$ns run
OUT PUT
4. Evaluate the performance of Drop Tail and RED queue management schemes
10/
#Open the NAM trace file
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exit 0
11/
#Give node position (for NAM)
12/
#Setup a CBR over UDP connection
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
$ns run
OUT PUT
13/
5. Evaluate the performance of CBQ and FQ Scheduling Mechanisms
$ns rtproto DV
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exit 0
14/
#Create seven nodes
#Schedule events for the CBR agent and the network dynamics
15/
$ns at 4.5 "$cbr0 stop"
$ns run
OUT PUT
16/
#Create 5 nodes
proc finish {} {
global f0 f1 f2
close $f0
close $f1
close $f2
exit 0
#'node' and attaches an Expoo traffic generator to the agent with the
#characteristic values 'size' for packet size 'burst' for burst time,
17/
#'idle' for idle time and 'rate' for burst peak rate. The procedure connects
#the source with the previously defined traffic sink 'sink' and returns the
#source object.
return $traffic
#three traffic sinks sink0/1/2 and writes it to the three files f0/1/2.
proc record {} {
18/
#Get an instance of the simulator
#Set the time after which the procedure should be called again
19/
set sink2 [new Agent/LossMonitor]
$ns run
20/
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
21/
$tcp set fid_ 1
OUT PUT
22/
7, (10.) Evaluate the performance of IEEE 802.11 and IEEE 802.15.4
# Define options
23/
set val(rp) AODV ;# routing protocol
create-god $val(nn)
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
24/
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
# Node Creation
$node1 set Z_ 0
$node2 set Z_ 0
25/
$ns initial_node_pos $node1 30
proc stop {} {
$ns flush-trace
close $namfile
close $tracefile
#Starting scheduler
$ns run
8(15.) Analysis of HTTP, DNS and DHCP Protocols. 10, 11 EXPERMENT PROGRAM
#Lan simulation
26/
#open tracefiles
proc finish {} {
$ns flush-trace
close $tracefile1
close $namfile
exit 0
27/
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail
MAC/Csma/Cd Channel]
28/
set udp [new Agent/UDP]
global ns
29/
}
$ns run
30/