You are on page 1of 2

set ns [new Simulator] ; #creating a new simulator variable ns

set tf [open p1.tr w] ; #open trace file p1.tr using a pointer tf which points to
trace file in writable mode
$ns trace-all $tf ; #main class object ns linked with trace file object tf to
dump all outputs in trace file

set nf [open p1.nam w] ; #open nam file p1.nam in writable mode using
pointer nf
$ns namtrace-all $nf ; #main class object ns linked with nam file object nf to
dump animation outputs

set n0 [$ns node] ; #creation of 4 nodes n0,n1,n2,n3


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$ns duplex-link $n0 $n2 20Mb 10ms DropTail ;#forming duplex connection between
the nodes no and n2 of bandwidth 20 MB
$ns duplex-link $n1 $n2 10Mb 10ms DropTail ; #forming duplex connection between
the nodes n1 and n2 of bandwidth 10 MB
$ns duplex-link $n2 $n3 0.7Mb 10ms DropTail ;#forming duplex connection between
the nodes n2 and n3 of bandwidth 0.7MB

$ns set queue-limit $n0 $n2 10 ;#Assigning Queuesize between the nodes no
and n2 as 10
$ns set queue-limit $n1 $n2 10 ;#Assigning Queuesize between the nodes n1
and n2 as 10
$ns set queue-limit $n2 $n3 5 ;#Assigning Queuesize between the nodes n2
and n3 as 5

set udp0 [new Agent/UDP] ;#creating new source agents


UDP 0 and udp 1(user datagram protocol)
set udp1 [new Agent/UDP]
set null [new Agent/Null] ;#creating Destination object(agent) for the class Agent/Null
set cbr0 [new Application/Traffic/CBR] ;#creating new traffic of Constant Bit rate
set cbr1 [new Application/Traffic/CBR]

$ns attach-agent $n0 $udp0 ;#Attaching agents to respective nodes i.e udp
0 to n0
$ns attach-agent $n1 $udp1 ;#n1 to udp 1
$ns attach-agent $n2 $null ;#n2 to destination null
$ns attach-agent $n3 $null ;#n3 to destination null
$cbr0 attach-agent $udp0 ;# attach traffic agents cbr0 and cbr1 to udp0 and
udp1
$cbr1 attach-agent $udp1

$ns connect $udp0 $null ;#connecting source and destination node


agents ie udp0 and udp1 to null
$ns connect $udp1 $null

$cbr0 set packetSize_ 512 ;#assigning values of packet size and time interval packet size
is 512 bytes and interval is 0.001s
$cbr0 set interval_ 0.001
$cbr1 set packetSize_ 512
$cbr1 set interval_ 0.005

proc finish { } { ;#terminate Simulation


global ns nf tf
$ns flush-trace ;#flushes output into trace file
close $tf ;#close tracefile
close $nf ;#close nam file
exec nam p1.nam & ;#excecute to get nam window
exit 0
}

$ns at 0.0 "$cbr0 start" ;#Setting time for start and stop the packet
transmission.at 0s no will start
$ns at 10.0 "$cbr0 stop" ;#at 10s n0 will stop transmission
$ns at 2.0 "$cbr1 start" ;#at 2 sn1 will start
$ns at 12.0 "$cbr1 stop" ;#at 12 s n2 will stop tramsmission
$ns at 13.0 "finish" ;#finish simulation

$ns run

You might also like