NS2记录TCP流的窗口大小
说明
下列代码将tcp_ 数组中每一条流的窗口大小每隔0.02秒进行一次采样,将采样时间和每一条流的窗口大小保存到output_数组对应的输出文件中。其中全局变量ns为模拟器,n_con为tcp流的数量。
记录代码
proc record { tcp_ output_ } {
upvar $tcp_ tcp
upvar $output_ output
global ns n_con
#set the time after which the procedure should be called again.
set time 0.02
#get the current time
set now [$ns now]
for {set i 0} {$i < $n_con } {incr i} {
puts $output($i) "$now [$tcp($i) set cwnd_]"
}
$ns at [expr $now+$time ] "record tcp output"
}
# Schedule events for the FTP agents
$ns at 0.0 "record tcp output"
运行结果绘制的图形
gnuplot
plot 'output0.ns' w lines lc 1 , 'output1.ns' w lines lc 2,'output2.ns' w lines lc 3 ,'output3.ns' w lines lc 4
完整代码如下:
#number of connections
set n_con 4
set ns [new Simulator]
#Define a 'finish' procedure
proc finish {output_ } {
upvar $output_ output
global ns n_con
for {set i 0} {$i< $n_con } {incr i} {
close $output($i)
}
$ns flush-trace
exit 0
}
proc record { tcp_ output_ } {
upvar $tcp_ tcp
upvar $output_ output
global ns n_con
#set the time after which the procedure should be called again.
set time 0.02
#get the current time
set now [$ns now]
for {set i 0} {$i < $n_con } {incr i} {
puts $output($i) "$now [$tcp($i) set cwnd_]"
}
$ns at [expr $now+$time ] "record tcp output"
}
# Create four node
set r0 [$ns node]
set r1 [$ns node]
#Create links between the nodes
$ns duplex-link $r0 $r1 1.5Mb 40ms DropTail
#set the queue size of link(r0-r1) to 20
$ns queue-limit $r0 $r1 20
for {set i 0} {$i < $n_con} {incr i} {
set s($i) [$ns node]
set d($i) [$ns node]
$ns duplex-link $s($i) $r0 10Mb 0.4ms DropTail
$ns duplex-link $r1 $d($i) 10Mb 0.4ms DropTail
set output($i) [open output$i.ns w]
}
for {set i 0} {$i < $n_con} {incr i} {
set tcp($i) [new Agent/TCP]
set sink($i) [new Agent/TCPSink]
$ns attach-agent $s($i) $tcp($i)
$ns attach-agent $d($i) $sink($i)
$ns connect $tcp($i) $sink($i)
$tcp($i) set fid_ $i
$tcp($i) set window_ 128
$tcp($i) set packetSize 512
#Setup a FTP over TCP Connection
set ftp($i) [new Application/FTP]
$ftp($i) attach-agent $tcp($i)
$ftp($i) set type_ FTP
}
# Schedule events for the FTP agents
$ns at 0.0 "record tcp output"
#$ns at 0.0 "$ftp(0) start"
for {set i 0} {$i < $n_con} {incr i} {
$ns at 0.0 "$ftp($i) start"
}
$ns at 30.0 "finish output"
#Run the simulation
$ns run