Vous êtes sur la page 1sur 9

set ns [new Simulator] # # Create a simple six node topology: # # s1 s3 # \ / # 10Mb,2ms \ 1.

5Mb,20ms / 10Mb,4ms # r1 --------- r2 # 10Mb,3ms / \ 10Mb,5ms # / \ # s2 s4 # set node_(s1) [$ns node] set node_(s2) [$ns node] set node_(r1) [$ns node] set node_(r2) [$ns node] set node_(s3) [$ns node] set node_(s4) [$ns node] $ns $ns $ns $ns $ns $ns $ns $ns $ns $ns $ns $ns $ns $ns duplex-link duplex-link duplex-link queue-limit queue-limit duplex-link duplex-link $node_(s1) $node_(s2) $node_(r1) $node_(r1) $node_(r2) $node_(s3) $node_(s4) $node_(r1) $node_(r1) $node_(r2) $node_(r2) $node_(r1) $node_(r2) $node_(r2) 10Mb 2ms DropTail 10Mb 3ms DropTail 1.5Mb 20ms RED 25 25 10Mb 4ms DropTail 10Mb 5ms DropTail orient right-down orient right-up orient right queuePos 0 queuePos 0 orient left-down orient left-up

duplex-link-op duplex-link-op duplex-link-op duplex-link-op duplex-link-op duplex-link-op duplex-link-op

$node_(s1) $node_(s2) $node_(r1) $node_(r1) $node_(r2) $node_(s3) $node_(s4)

$node_(r1) $node_(r1) $node_(r2) $node_(r2) $node_(r1) $node_(r2) $node_(r2)

set tcp1 [$ns create-connection TCP/Reno $node_(s1) TCPSink $node_(s3) 0] $tcp1 set window_ 15

set tcp2 [$ns create-connection TCP/Reno $node_(s2) TCPSink $node_(s3) 1] $tcp2 set window_ 15 set ftp1 [$tcp1 attach-source FTP] set ftp2 [$tcp2 attach-source FTP] # Tracing a queue set redq [[$ns link $node_(r1) $node_(r2)] queue] set tchan_ [open all.q w] $redq trace curq_ $redq trace ave_ $redq attach $tchan_ $ns at 0.0 "$ftp1 start" $ns at 3.0 "$ftp2 start" $ns at 10 "finish" # Define 'finish' procedure (include post-simulation processes) proc finish {} { global tchan_ set awkCode { { if ($1 == "Q" && NF>2) { print $2, $3 >> "temp.q"; set end $2 } else if ($1 == "a" && NF>2) print $2, $3 >> "temp.a"; } } set f [open temp.queue w] puts $f "TitleText: red" puts $f "Device: Postscript" if { [info exists tchan_] } { close $tchan_ } exec rm -f temp.q temp.a exec touch temp.a temp.q exec awk $awkCode all.q 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 & exit 0 } $ns run

Cau 1: set ns [new Simulator] set trf [open out.tr w] $ns trace-all $trf proc finish { } { global ns trf $ns flush-trace close $trf exit 0 } set set set set s1 s2 s3 s4 [$ns [$ns [$ns [$ns node] node] node] node]

set r1 [$ns node] set r2 [$ns node] #link 1 $ns duplex-link #link 2 $ns duplex-link #link 3 $ns duplex-link #link 4 $ns duplex-link #link 5 $ns duplex-link $s1 $r1 1.5Mb 20ms DropTail $s2 $r1 10Mb 5ms DropTail $r1 $r2 1.5Mb 20ms DropTail $s3 $r2 1.5Mb 20ms DropTail $s4 $r2 10Mb 5ms DropTail

set tcp1 [new Agent/TCP] $tcp1 set fid_ 1

$ns attach-agent $s1 $tcp1 set sink1 [new Agent/TCPSink] $ns attach-agent $s3 $sink1 $ns connect $tcp1 $sink1 set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1 $ftp1 set type_ FTP $ns at 0.1 "$ftp1 start" $ns at 10.1 "$ftp1 stop" set tcp2 [new Agent/TCP] $tcp2 set fid_ 2 $ns attach-agent $s2 $tcp2 set sink2 [new Agent/TCPSink] $ns attach-agent $s4 $sink2 $ns connect $tcp2 $sink2 set ftp2 [new Application/FTP] $ftp2 attach-agent $tcp2 $ftp2 set type_ FTP $ns at 5.1 "$ftp2 start" $ns at 9.1 "$ftp2 stop" $ns at 10.2 "finish" $ns run Cau 2: #Writen by Toan M. Ha #wired trace file format # 0 -------- 2 # # 4-------5 # # 1 -------- 3 BEGIN { # khoi tao cac thanh phan trong sent_pkt, recv_pkt for (i in sent_pkt) { sent_pkt[i] = 0; } total_delay_ftp1 = 0 total_delay_ftp2 = 0 num_pkt_ftp1 = 0 num_pkt_ftp2 = 0 data_ftp1 = 0 data_ftp2 = 0 } { # su kien gui goi tin TCP if($1 == "-" && $5 == "tcp"){ # xay ra tai s1, s2 => ghi vao trong de tinh delay if($3 == "0" || $3 = "1") {

sent_pkt[$12] = $2 } } # su kien nhan goi tin TCP if($1 == "r" && $5 == "tcp"){ #nhan tai s3: tinh tong thong luong + delay if($4 == "2"){ data_ftp1 += $6 if(sent_pkt[$12] > 0){ total_delay_ftp1 += $2 - sent_pkt[$12] num_pkt_ftp1 ++ } } #nhan tai s4: tinh tong thong luong + delay if($4 == "3"){ data_ftp2 += $6 if(sent_pkt[$12] > 0){ total_delay_ftp2 += $2 - sent_pkt[$12] num_pkt_ftp2 ++ } } } } END {

Cau 3_s3: #Writen by Toan M. Ha #wired trace file format # 0 -------- 2 # # 4-------5 # # 1 -------- 3 BEGIN { data_ftp = 0 start_time = 0 } { # su kien nhan goi tin TCP if($1 == "r" && $5 == "tcp"){ #nhan tai s3: tinh tong thong luong if($4 == "2"){ data_ftp += $6 if(start_time == 0) { start_time = $2 } else { printf("%f\t%f\n",$2,(data_ftp*8)/(($2-start_time) * 1024 * 1024))

} } } } END { } Cau 3_s4: #Writen by Toan M. Ha #wired trace file format # 0 -------- 2 # # 4-------5 # # 1 -------- 3 BEGIN { data_ftp = 0 start_time = 0 } { # su kien nhan goi tin TCP if($1 == "r" && $5 == "tcp"){ #nhan tai s3: tinh tong thong luong if($4 == "3"){ data_ftp += $6 if(start_time == 0) { start_time = $2 } else { printf("%f\t%f\n",$2,(data_ftp*8)/(($2-start_time) * 1024 * 1024)) } } } } END { }

Bai 3

#Create a simulator object set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set nf [open out.nam w] $ns namtrace-all $nf #Define a 'finish' procedure proc finish {} { global ns nf $ns flush-trace #Close the NAM trace file close $nf #Execute NAM on the trace file exec nam out.nam & exit 0 } #Create four nodes set n0 [$ns node] set n1 [$ns node]

set n2 [$ns node] set n3 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail #Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 10 #Give node position (for NAM) $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 #Monitor the queue for link (n2-n3). (for NAM) $ns duplex-link-op $n2 $n3 queuePos 0.5 #Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink $ns connect $tcp $sink $tcp set fid_ 1 #Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP #Setup a UDP connection 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 #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false #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" #Detach tcp and sink agents (not really necessary) $ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink" #Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish" #Print CBR packet size and interval puts "CBR packet size = [$cbr set packet_size_]" puts "CBR interval = [$cbr set interval_]" #Run the simulation $ns run Anh mau