You are on page 1of 14

Lab1-1

set ns [new Simulator]


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

proc finish { } {
global ns myNAM
$ns flush-trace
close $myNAM
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 $n4 100Mb 10ms DropTail


$ns duplex-link $n1 $n4 100Mb 10ms DropTail
$ns duplex-link $n2 $n4 54Mb 10ms DropTail
$ns duplex-link $n3 $n4 54Mb 10ms DropTail
set udp [new Agent/UDP]

$ns attach-agent $n1 $udp


set null [new Agent/Null]
$ns attach-agent $n2 $null
$ns connect $udp $null

set cbr [new Application/Traffic/CBR]


$cbr attach-agent $udp

$cbr set packetSize_ 1000


$cbr set rate_ 2Mb

set tcp [new Agent/TCP]


$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 2

set ftp [new Application/FTP]


$ftp attach-agent $tcp
$ns at 0.05 "$ftp start"
$ns at 0.1 "$cbr start"
$ns at 60.0 "$ftp stop"
$ns at 60.5 "$cbr stop"
$ns at 61 "finish"

# Start the simulation


$ns run

LAB 1-2

2-1

set ns [new Simulator]


set mytrace [open out.tr w]
$ns trace-all $mytrace
set myNAM [open out.nam w]
$ns namtrace-all $myNAM

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

set n0 [$ns node]


set n1 [$ns node]

$ns duplex-link $n0 $n1 1.5Mb 100ms DropTail


$ns queue-limit $n0 $n1 10

set tcp [new Agent/TCP]


$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $sink
$ns connect $tcp $sink
$tcp set fid_ 1

set ftp [new Application/FTP]


$ftp attach-agent $tcp

$ns at 0.05 "$ftp start"

$ns at 60.0 "$ftp stop"

$ns at 61 "finish"

$ns run

2-2
set ns [new Simulator]
set nt [open out.nam w]

$ns namtrace-all $nt

proc finish {} {
global ns nt

$ns flush-trace

close $nt

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]
set n5 [$ns node]
set n6 [$ns node]
set n7 [$ns node]
set n8 [$ns node]
set n9 [$ns node]
set n10 [$ns node]
set n11 [$ns node]

$ns duplex-link $n0 $n1 1.5Mb 10.0ms DropTail


$ns queue-limit $n0 $n1 10
$ns duplex-link $n2 $n0 1.5Mb 10.0ms DropTail
$ns duplex-link $n3 $n0 1.5Mb 10.0ms DropTail
$ns duplex-link $n4 $n0 1.5Mb 10.0ms DropTail
$ns duplex-link $n5 $n0 1.5Mb 10.0ms DropTail
$ns duplex-link $n6 $n0 1.5Mb 10.0ms DropTail
$ns duplex-link $n7 $n1 1.5Mb 10.0ms DropTail
$ns duplex-link $n8 $n1 1.5Mb 10.0ms DropTail
$ns duplex-link $n9 $n1 1.5Mb 10.0ms DropTail
$ns duplex-link $n10 $n1 1.5Mb 10.0ms DropTail
$ns duplex-link $n11 $n1 1.5Mb 10.0ms DropTail

$ns duplex-link-op $n0 $n1 orient right


$ns duplex-link-op $n2 $n0 orient down
$ns duplex-link-op $n3 $n0 orient right-down
$ns duplex-link-op $n4 $n0 orient right
$ns duplex-link-op $n5 $n0 orient right-up
$ns duplex-link-op $n6 $n0 orient up
$ns duplex-link-op $n7 $n1 orient down
$ns duplex-link-op $n8 $n1 orient left-down
$ns duplex-link-op $n9 $n1 orient left
$ns duplex-link-op $n10 $n1 orient left-up
$ns duplex-link-op $n11 $n1 orient up

$ns color 1 Violet


$ns color 2 Indigo
$ns color 3 Blue$ns color 4 Green
$ns color 5 Yellow

set tcp0 [new Agent/TCP]

$ns attach-agent $n2 $tcp0


set sink0 [new Agent/TCPSink]
$ns attach-agent $n7 $sink0
$ns connect $tcp0 $sink0
$tcp0 set fid_ 1
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 0.0 "$ftp0 start"
$ns at 5.0 "$ftp0 stop"
set tcp1 [new Agent/TCP]
$ns attach-agent $n3 $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n8 $sink1
$ns connect $tcp1 $sink1
$tcp1 set fid_ 2
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 0.0 "$ftp1 start"
$ns at 5.0 "$ftp1 stop"
set tcp2 [new Agent/TCP]
$ns attach-agent $n4 $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $n9 $sink2
$ns connect $tcp2 $sink2
$tcp2 set fid_ 3
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ns at 0.0 "$ftp2 start"
$ns at 5.0 "$ftp2 stop"
set tcp3 [new Agent/TCP]
$ns attach-agent $n5 $tcp3

set sink3 [new Agent/TCPSink]


$ns attach-agent $n10 $sink3
$ns connect $tcp3 $sink3
$tcp3 set fid_ 4
set ftp3 [new Application/FTP]
$ftp3 attach-agent $tcp3
$ns at 0.0 "$ftp3 start"
$ns at 5.0 "$ftp3 stop"
set tcp4 [new Agent/TCP]
$ns attach-agent $n6 $tcp4
set sink4 [new Agent/TCPSink]
$ns attach-agent $n11 $sink4
$ns connect $tcp4 $sink4
$tcp4 set fid_ 5
set ftp4 [new Application/FTP]
$ftp4 attach-agent $tcp4
$ns at 0.0 "$ftp4 start"
$ns at 5.0 "$ftp4 stop"

$ns at 5 "finish"
$ns run

2-3

Lab 3-1

set ns [new Simulator]


set nf [open out.nam w]
$ns namtrace-all $nf
set r1 [$ns node]
set r2 [$ns node]
set s1 [$ns node]
set s2 [$ns node]
set s3 [$ns node]
set s4 [$ns node]
$ns duplex-link $r1 $r2 1.5Mb 20ms RED
$ns queue-limit $r1 $r2 25
$ns duplex-link $s1 $r1 10Mb 2ms DropTail
$ns duplex-link $s2 $r1 10Mb 3ms DropTail
$ns duplex-link $s3 $r2 10Mb 4ms DropTail
$ns duplex-link $s4 $r2 10Mb 5ms DropTail
$ns duplex-link-op $r1 $r2 orient right
$ns duplex-link-op $r2 $s3 orient right-up
$ns duplex-link-op $r2 $s4 orient right-down
$ns duplex-link-op $r1 $s1 orient left-up
$ns duplex-link-op $r1 $s2 orient left-down
set tcp1 [new Agent/TCP]
$ns attach-agent $s1 $tcp1
$tcp1 set max_win_ 15
$tcp1 set fid_ 1
set sink1 [new Agent/TCPSink]
$ns attach-agent $s3 $sink1
$ns connect $tcp1 $sink1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1

set tcp2 [new Agent/TCP]


$ns attach-agent $s2 $tcp2
$tcp2 set max_win_ 15
$tcp2 set fid_ 2
set sink2 [new Agent/TCPSink]
$ns attach-agent $s3 $sink2
$ns connect $tcp2 $sink2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
set req [[$ns link $r1 $r2] queue ]
set ch [open all.q w]
$req trace ave_
$req trace curq_
$req attach $ch
proc finish {} {
global ns nf ch
$ns flush-trace
close $nf
set awkCode {
{
if ($1 == "Q" && NF>2) {
print $2, $3 >> "temp.q";
}
else if ($1 == "a" && NF>2)
print $2, $3 >> "temp.a";
}
}
set f [open temp.queue w]
exec rm -f temp.q temp.a
exec touch temp.a temp.q
exec awk $awkCode all.q

close $ch
puts $f \"queue
exec cat temp.q >@ $f
puts $f \n\"ave_queue
exec cat temp.a >@ $f
close $f
exec xgraph -bb -tk -x time -y queue temp.queue &
exec nam out.nam &
exit 0
}
$ns at 0 "$ftp1 start"
$ns at 0 "$ftp2 start"
$ns at 5 "$ftp1 stop"
$ns at 5 "$ftp2 stop"
$ns at 5 "finish"
$ns run

Lab 3-2

You might also like