Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Vivek Kumar 106121146

NETWORKS LAB 4
Q1. Simulate any 3 topologies (of which one must be hybrid topology) with N
nodes for 100secs. The nodes are numbered sequentially starting from 1. In all the
topologies, the odd numbered stations are sources and even numbered nodes are
destinations. Assume that all source nodes transmit the fixed size data packets from
the start of the simulation till the end of the simulation.
a) Ring Topology
# Create a new NS2 simulation instance
set ns [new Simulator]
# Set simulation time to 100 seconds
$ns at 100.0 "finish"
# Create a ring topology with N nodes
set N 10 ;
for {set i 1} {$i <= $N} {incr i} {
set node($i) [$ns node]
}
# Define link properties
set bw 1Mbps ;# Bandwidth
set delay 10ms ;# Propagation delay
# Create links to form a ring
for {set i 1} {$i <= $N} {incr i} {
$ns duplex-link $node($i) $node([expr ($i % $N) + 1]) $bw $delay
DropTail
}
# Generate traffic from odd-numbered nodes to even-numbered
nodes
Vivek Kumar 106121146

for {set i 1} {$i <= $N} {incr i} {


if { $i % 2 == 1 } {
set src $node($i)
set dst $node([expr ($i % $N) + 2])
# Create a traffic source and attach it to the source node
set udp [new Agent/UDP]
$ns attach-agent $src $udp
# Create a CBR traffic generator
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize 1000 ;# Change packet size as needed
$cbr set interval 0.1s ;# Change interval as needed
# Start the traffic generation at time 1.0 seconds
$ns at 1.0 "$cbr start"
}
}
# Define a procedure to finish the simulation
proc finish {} {
global ns
$ns flush-trace
$ns finish
}
# Run the simulation
$ns run
Vivek Kumar 106121146
Vivek Kumar 106121146

b) Mesh Topology
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
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n3 $n0 1Mb 10ms DropTail
$ns duplex-link $n0 $n4 1Mb 10ms DropTail
$ns duplex-link $n1 $n4 1Mb 10ms DropTail
$ns duplex-link $n2 $n4 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
Vivek Kumar 106121146

set tcp [new Agent/TCP]


$tcp set class_ 1
set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
set tcp2 [new Agent/TCP]
$tcp2 set class_ 3
set tcp3 [new Agent/TCP]
$tcp3 set class_ 4
set tcp4 [new Agent/TCP]
$tcp4 set class_ 5
set tcp5 [new Agent/TCP]
$tcp5 set class_ 6
set tcp6 [new Agent/TCP]
$tcp6 set class_ 7
set tcp7 [new Agent/TCP]
$tcp7 set class_ 8
set tcpsink [new Agent/TCPSink]
set tcpsink1 [new Agent/TCPSink]
set tcpsink2 [new Agent/TCPSink]
set tcpsink3 [new Agent/TCPSink]
set tcpsink4 [new Agent/TCPSink]
set tcpsink5 [new Agent/TCPSink]
set tcpsink6 [new Agent/TCPSink]
set tcpsink7 [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
Vivek Kumar 106121146

$ns attach-agent $n1 $tcpsink


$ns attach-agent $n1 $tcp1
$ns attach-agent $n2 $tcpsink1
$ns attach-agent $n2 $tcp2
$ns attach-agent $n3 $tcpsink2
$ns attach-agent $n3 $tcp3
$ns attach-agent $n0 $tcpsink3
$ns attach-agent $n4 $tcp4
$ns attach-agent $n0 $tcpsink4
$ns attach-agent $n4 $tcp5
$ns attach-agent $n1 $tcpsink5
$ns attach-agent $n4 $tcp6
$ns attach-agent $n2 $tcpsink6
$ns attach-agent $n4 $tcp7
$ns attach-agent $n3 $tcpsink7
$ns connect $tcp $tcpsink
$ns connect $tcp1 $tcpsink1
$ns connect $tcp2 $tcpsink2
$ns connect $tcp3 $tcpsink3
$ns connect $tcp4 $tcpsink4
$ns connect $tcp5 $tcpsink5
$ns connect $tcp6 $tcpsink6
$ns connect $tcp7 $tcpsink7
set ftp [new Application/FTP]
$ftp attach-agent $tcp
Vivek Kumar 106121146

set ftp1 [new Application/FTP]


$ftp1 attach-agent $tcp1
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
set ftp3 [new Application/FTP]
$ftp3 attach-agent $tcp3
set ftp4 [new Application/FTP]
$ftp4 attach-agent $tcp4
set ftp5 [new Application/FTP]
$ftp5 attach-agent $tcp5
set ftp6 [new Application/FTP]
$ftp6 attach-agent $tcp6
set ftp7 [new Application/FTP]
$ftp7 attach-agent $tcp7
Vivek Kumar 106121146
Vivek Kumar 106121146

c) Star Topology
set ns [new Simulator]
$ns color 1 blue
$ns color 2 red
set lim 9
set nf [open star.nam w]
$ns namtrace-all $nf
set f [open star.tr w]
$ns trace-all $f
proc finish {} {
global ns nf f
$ns flush-trace
close $nf
close $f
exec nam star.nam &
exit 0
}
for {set i 0} {$i<$lim} {incr i} {
set n($i) [$ns node]
}
for {set i 1} {$i<$lim} {incr i} {
$ns duplex-link $n(0) $n($i) 4Mb 10ms SFQ
}
106121094
Vivek Kumar 106121146

$ns color 2 red


for {set i 1} {$i<$lim} {incr i 2} {
# establishing tcp connection between n1 to n4 through n0
set tcp($i) [new Agent/TCP]
$tcp($i) set class_ 2
$ns attach-agent $n($i) $tcp($i)
set sink($i) [new Agent/TCPSink]
$ns attach-agent $n([expr $i+1]) $sink($i)
$ns connect $tcp($i) $sink($i)
# generating ftp traffic
set ftp($i) [new Application/FTP]
$ftp($i) attach-agent $tcp($i)
$ftp($i) set type_ FTP
$ftp($i) set packet_size_ 1000
$ftp($i) set rate_ 1mb
}
for {set i 1} {$i<$lim} {incr i 2} {
$ns at 0.0 "$ftp($i) start"
$ns at 10.0 "$ftp($i) stop"
}
$ns at 11.0 "finish"
$ns run
Vivek Kumar 106121146

You might also like