NS2记录TCP流的窗口大小

该博客介绍了如何使用NS2模拟器记录TCP流的窗口大小。通过定义`record`过程,每0.02秒采样一次每个连接的窗口大小,并将数据写入对应文件。最后,利用gnuplot绘制了各输出文件的图形,展示TCP流的窗口变化。整个模拟创建了四个TCP连接,每个连接采用FTP应用,并在0秒时启动。整个模拟在30秒后结束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值