#create simulator
set ns [new Simulator]
#set one communication line as blue and 2nd as red
$ns color 1 Blue
$ns color 2 Red
#create and open out.nam file in write mode
set nf [open out.nam w]
$ns namtrace-all $nf
#also create its trace file as testout.tr
set tf [open testout.tr w]
$ns trace-all $tf
#create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#define transaction (format node1 node2 Bandwidth delay queue)
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 20ms DropTail
#set queue size for node2 and node3 to 10pkts
$ns queue-limit $n2 $n3 10
#draw the links to the node with specified orientations
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
#set a tcp connection, agent object between(node1 source and node2 sink)
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_ 1
#set a fip FTP (file transfer protocol)
set ftp [new Application/FTP]
$ftp attach-agent $tcp
#set a UDP (User datagram protocol)
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
#se a CBR (Constant Bit Rate) over UDP Connection, okt bytes, rate
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packet_size_ 1000
$cbr set rate_ 1Mb
#schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
#finish handler
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
#call finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulator
$ns run