To drop packets
Posted: Sun Sep 11, 2011 9:56 am
how do i write a basic tcl script in ns2 to drop packets in a wired scenario just by changing bandwidth n delay values.
this is my script:
this is my script:
Code: Select all
set val(stop) 7.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#Open the NS trace file
set tracefile [open wr5nodes.tr w]
$ns trace-all $tracefile
#Open the NAM trace file
set namfile [open wr5nodes.nam w]
$ns namtrace-all $namfile
#===================================
# Nodes Definition
#===================================
#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n1 100.0Mb 10ms DropTail
$ns queue-limit $n0 $n1 50
$ns duplex-link $n1 $n2 100.0Mb 10ms DropTail
$ns queue-limit $n1 $n2 50
$ns duplex-link $n1 $n3 100.0Mb 10ms DropTail
$ns queue-limit $n1 $n3 50
$ns duplex-link $n1 $n4 100.0Mb 10ms DropTail
$ns queue-limit $n1 $n4 50
#Give node position (for NAM)
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n1 $n3 orient right
$ns duplex-link-op $n1 $n4 orient right-down
set tcp [new Agent/TCP]
set tcpsink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
$ns attach-agent $n1 $tcpsink
$ns connect $tcp $tcpsink
set tcp1 [new Agent/TCP]
set tcpsink1 [new Agent/TCPSink]
$ns attach-agent $n1 $tcp1
$ns attach-agent $n2 $tcpsink1
$ns connect $tcp1 $tcpsink1
set tcp2 [new Agent/TCP]
set tcpsink2 [new Agent/TCPSink]
$ns attach-agent $n1 $tcp2
$ns attach-agent $n4 $tcpsink2
$ns connect $tcp2 $tcpsink2
set tcp3 [new Agent/TCP]
set tcpsink3 [new Agent/TCPSink]
$ns attach-agent $n2 $tcp3
$ns attach-agent $n1 $tcpsink3
$ns connect $tcp3 $tcpsink3
set tcp4 [new Agent/TCP]
set tcpsink4 [new Agent/TCPSink]
$ns attach-agent $n1 $tcp4
$ns attach-agent $n0 $tcpsink4
$ns connect $tcp4 $tcpsink4
set tcp5 [new Agent/TCP]
set tcpsink5 [new Agent/TCPSink]
$ns attach-agent $n0 $tcp5
$ns attach-agent $n1 $tcpsink5
$ns connect $tcp5 $tcpsink5
set tcp6 [new Agent/TCP]
set tcpsink6 [new Agent/TCPSink]
$ns attach-agent $n1 $tcp6
$ns attach-agent $n3 $tcpsink6
$ns connect $tcp6 $tcpsink6
set tcp7 [new Agent/TCP]
set tcpsink7 [new Agent/TCPSink]
$ns attach-agent $n4 $tcp7
$ns attach-agent $n1 $tcpsink7
$ns connect $tcp7 $tcpsink7
set tcp8 [new Agent/TCP]
set tcpsink8 [new Agent/TCPSink]
$ns attach-agent $n2 $tcp8
$ns attach-agent $n1 $tcpsink8
$ns connect $tcp8 $tcpsink8
set tcp9 [new Agent/TCP]
set tcpsink9 [new Agent/TCPSink]
$ns attach-agent $n1 $tcp9
$ns attach-agent $n0 $tcpsink9
$ns connect $tcp9 $tcpsink9
set cbr [new Application/Traffic/CBR]
set cbr1 [new Application/Traffic/CBR]
set cbr2 [new Application/Traffic/CBR]
set cbr3 [new Application/Traffic/CBR]
set cbr4 [new Application/Traffic/CBR]
set cbr5 [new Application/Traffic/CBR]
set cbr6 [new Application/Traffic/CBR]
set cbr7 [new Application/Traffic/CBR]
set cbr8 [new Application/Traffic/CBR]
set cbr9 [new Application/Traffic/CBR]
$cbr attach-agent $tcp
$cbr1 attach-agent $tcp1
$cbr2 attach-agent $tcp2
$cbr3 attach-agent $tcp3
$cbr4 attach-agent $tcp4
$cbr5 attach-agent $tcp5
$cbr6 attach-agent $tcp6
$cbr7 attach-agent $tcp7
$cbr8 attach-agent $tcp8
$cbr9 attach-agent $tcp9
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
}
$ns at 0.5 "$cbr start"
$ns at 1.0 "$cbr stop"
$ns at 1.0 "$cbr1 start"
$ns at 1.5 "$cbr1 stop"
$ns at 1.5 "$cbr2 start"
$ns at 2.0 "$cbr2 stop"
$ns at 2.0 "$cbr3 start"
$ns at 2.5 "$cbr3 stop"
$ns at 2.5 "$cbr4 start"
$ns at 3.0 "$cbr4 stop"
$ns at 3.5 "$cbr5 start"
$ns at 4.0 "$cbr5 stop"
$ns at 4.0 "$cbr6 start"
$ns at 4.5 "$cbr6 stop"
$ns at 5.0 "$cbr7 start"
$ns at 5.5 "$cbr7 stop"
$ns at 5.0 "$cbr8 start"
$ns at 5.5 "$cbr8 stop"
$ns at 5.5 "$cbr9 start"
$ns at 6.0 "$cbr9 stop"
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run