Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Expt_05_NW Topology NL Lab Roll No: 121A3031_

Experiment 05–Network Topology

Roll No. 121A3031


Name Yadhukrishna Nair
Class SE IT
Subject NL Lab
LO Mapped LO2:  ITL401.3 (Demonstrate and measure different network
scenarios and their performance behavior.)
Expt_05_NW Topology NL Lab Roll No: 121A3031_

Aim: Implementation of Specific Network topology with respect to Number of nodes and
physical layer configuration

Theory: 

In this section, we are going to develop a Tcl script for ns which simulates a simple topology. We 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 our simulation script to visualize our simulation.

1. How to start
Now we are going to write a 'template' that we can use for all of the first Tcl scripts. You can write
your Tcl scripts in any text editor like vi. This first example is 'example1.tcl'.

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

set ns [new Simulator]

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

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

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
$ns flush-trace
close $nf
exec nam out.nam &
exit 0 }

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

$ns at 5.0 "finish"

ns provides a very simple way to schedule events with the 'at' command.
The last line finally starts the simulation.
$ns run
We can actually save the file now and try to run it with 'ns example1.tcl'.

2. Two nodes, one link

In this section we are going to define a very simple topology with two nodes that are
Expt_05_NW Topology NL Lab Roll No: 121A3031_

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"').

set n0 [$ns node]


set n1 [$ns node]

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.

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

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

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.
Expt_05_NW Topology NL Lab Roll No: 121A3031_

#Create a UDP agent and attach it to node n0


set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

These lines create a UDP agent and attach it to the node n0, then attach a CBR traffic
generator to the UDP agent. CBR stands for 'constant bit rate'. Line 7 and 8 should be
selfexplaining. The packetSize is being set to 500 bytes and a packet will be sent every 0.005
seconds (i.e. 200 packets per second).
The next lines create a Null agent which acts as traffic sink and attach it to node n1.

set null0 [new Agent/Null]


$ns attach-agent $n1 $null0

Now the two agents have to be connected with each other.


$ns connect $udp0 $null0

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"'.

$ns at 0.5 "$cbr0 start"


$ns at 4.5 "$cbr0 stop"

This code should be self-explaining again.


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.
Expt_05_NW Topology NL Lab Roll No: 121A3031_

As the ns2 simulations work on tcl scripts so we should know about tcl

Conclusion : Thus we have written our script for two nodes network.

Code :
#——-Event scheduler object creation——–#
set ns [new Simulator]

#———-creating trace objects—————-#

set nt [open test2.tr w]


$ns trace-all $nt
#———-creating nam objects—————-#

set nf [open test2.nam w]


$ns namtrace-all $nf
#———-Setting color ID—————-#
$ns color 1 darkmagenta
$ns color 2 yellow
$ns color 3 blue
$ns color 4 green
$ns color 5 black

#———- Creating Network—————-#

set totalNodes 3

for {set i 0} {$i < $totalNodes} {incr i} {


set node_($i) [$ns node]
Expt_05_NW Topology NL Lab Roll No: 121A3031_

set server 0
set router 1
set client 2

#———- Creating Duplex Link—————-#


$ns duplex-link $node_($server) $node_($router) 2Mb 50ms DropTail
$ns duplex-link $node_($router) $node_($client) 2Mb 50ms DropTail

$ns duplex-link-op $node_($server) $node_($router) orient right


$ns duplex-link-op $node_($router) $node_($client) orient right

#————Labelling—————-#

$ns at 0.0 "$node_($server) label Server"


$ns at 0.0 "$node_($router) label Router"
$ns at 0.0 "$node_($client) label Client"

$ns at 0.0 "$node_($server) color orange"


$ns at 0.0 "$node_($client) color orange"

$node_($server) shape hexagon


$node_($client) shape hexagon
#————Data Transfer between Nodes—————-#

# Defining a transport agent for sending


set tcp [new Agent/TCP]

# Attaching transport agent to sender node


$ns attach-agent $node_($server) $tcp

# Defining a transport agent for receiving


set sink [new Agent/TCPSink]

# Attaching transport agent to receiver node


$ns attach-agent $node_($client) $sink

#Connecting sending and receiving transport agents


$ns connect $tcp $sink

#Defining Application instance


set ftp [new Application/FTP]

# Attaching transport agent to application agent


Expt_05_NW Topology NL Lab Roll No: 121A3031_

$ftp attach-agent $tcp

# Setting flow color


$tcp set fid_ 4

# data packet generation starting time


$ns at 1.0 "$ftp start"

# data packet generation ending time


$ns at 6.0 "$ftp stop"

#———finish procedure——–#

proc finish {} {
global ns nf nt
$ns flush-trace
close $nf
close $nt
puts "running nam…"
exec nam test2.nam &
exit 0
}

#Calling finish procedure


$ns at 10.0 "finish"
$ns run

Output:
Expt_05_NW Topology NL Lab Roll No: 121A3031_

Star Topology:
Code:
set ns  [new Simulator]

$ns color 1 blue


$ns color 2 red

$ns rtproto DV

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
    }

#creating Nodes        
for {set i 0} {$i<7} {incr i} {
set n($i) [$ns node]
}

#Creating Links
for {set i 1} {$i<7} {incr i} {
$ns duplex-link $n(0) $n($i) 512Kb 10ms SFQ
}

#Orienting The nodes


$ns duplex-link-op $n(0) $n(1) orient left-up
$ns duplex-link-op $n(0) $n(2) orient right-up
$ns duplex-link-op $n(0) $n(3) orient right
$ns duplex-link-op $n(0) $n(4) orient right-down
$ns duplex-link-op $n(0) $n(5) orient left-down
$ns duplex-link-op $n(0) $n(6) orient left

#TCP_Config
set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n(1) $tcp0
Expt_05_NW Topology NL Lab Roll No: 121A3031_

set sink0 [new Agent/TCPSink]


$ns attach-agent $n(4) $sink0

$ns connect $tcp0 $sink0

#UDP_Config
set udp0 [new Agent/UDP]
$udp0 set class_ 2
$ns attach-agent $n(2) $udp0

set null0 [new Agent/Null]


$ns attach-agent $n(5) $null0

$ns connect $udp0 $null0

#CBR Config
set cbr0 [new Application/Traffic/CBR]
$cbr0 set rate_ 256Kb
$cbr0 attach-agent $udp0

#FTP Config
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0

#Scheduling Events
$ns rtmodel-at 0.5 down $n(0) $n(5)
$ns rtmodel-at 0.9 up $n(0) $n(5)

$ns rtmodel-at 0.7 down $n(0) $n(4)


$ns rtmodel-at 1.2 up $n(0) $n(4)

$ns at 0.1 "$ftp0 start"


$ns at 1.5 "$ftp0 stop"

$ns at 0.2 "$cbr0 start"


$ns at 1.3 "$cbr0 stop"

$ns at 2.0 "finish"


$ns run
Expt_05_NW Topology NL Lab Roll No: 121A3031_

Output:

You might also like