ns 2.35 柯志亨书-实验3笔记-TCP UDP模拟

来源:互联网 发布:2016淘宝客推广 编辑:程序博客网 时间:2024/04/29 05:20

上图是笔记

下面是tcl代码:

#Create a simulator objectset ns [new Simulator]#Set different color for dif flow$ns color 1 Blue$ns color 2 Redset tracefd [open example1.tr w]$ns trace-all $tracefdset namtracefd [open example1.nam w]$ns namtrace-all $namtracefdproc finish {} {global ns tracefd namtracefd$ns flush-traceclose $tracefdclose $namtracefdexec nam example1.nam &exit 0}# Set nodes, s1's id is 0, s2'id is 1set s1 [$ns node]set s2 [$ns node]# Set router node, id of r is 2set r [$ns node]# Set dest node, id of r is 3set d [$ns node]# Set link parameters$ns duplex-link $s1 $r 2Mb   10ms DropTail$ns duplex-link $s2 $r 2Mb   10ms DropTail$ns duplex-link $r  $d 1.7Mb 20ms DropTail# Set Queue limit 10 for r and d$ns queue-limit $r $d 10# Set Node pos for NAM$ns duplex-link-op $s1 $r orient right-down$ns duplex-link-op $s2 $r orient right-up$ns duplex-link-op $r  $d orient right# Observe the change of queue between r and d for NAM$ns duplex-link-op $r $d queuePos 0.5# Setup TCP agent and FTP trafficset tcp [new Agent/TCP]$ns attach-agent $s1 $tcpset sink [new Agent/TCPSink]$ns attach-agent $d $sink$ns connect $tcp $sink# In NAM, TCP will diplay in Blue$tcp set fid_ 1set ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP# Setup a UDP Agent and CBR Trafficset udp [new Agent/UDP]$ns attach-agent $s2 $udpset null [new Agent/Null]$ns attach-agent $d $null$ns connect $udp $null$udp set fid_ 2set cbr [new Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packetSize_ 1000$cbr set rate_ 1mb$cbr set random_ false# Setup time line$ns at 0.1 "$cbr start"$ns at 1.0 "$ftp start"$ns at 4.0 "$ftp stop"$ns at 4.5 "$cbr stop"$ns at 5.0 "finish"$ns run

运行tcl后,会生.tr文件,利用awk分析.tr文件,awk代码如下,计算cbr_delay的awk

# Measure the end to end delay by the trace fileBEGIN{# program initializehighest_packet_id = 0;}{# awk会自动循环执行这个{}action = $1;time = $2;from = $3;to = $4;type = $5;pktsize = $6;flow_id = $8;src = $9;dst = $10;seq_no = $11;packet_id = $12;# Record the current max packet IDif ( packet_id > highest_packet_id )highest_packet_id = packet_id;# Record the tx time of packetif ( start_time[packet_id] == 0 )start_time[packet_id] = time;# Record CBR flow_id=2 rx time# 这里既要判断flow=2,没有drop,还要判断recv# drop是必须的,因为有可能1-2 recv,2-3 drop了# CBR 路径是1-2-3,整条路径上都有可能dropif ( flow_id == 2 && action != "d" ){if (action == "r"){end_time[packet_id] = time;}}elseend_time[packet_id] = -1;}END {# When read over, start to calculatefor ( packet_id=0; packet_id<=highest_packet_id; packet_id++ ){start = start_time[packet_id];end = end_time[packet_id];duration = start-end;if (start<end)printf("%f %f\n", start, duration);}}

执行awk的shell文件如下:

#!/bin/bashawk -f cbr_delay.awk example1.tr > cbr_delay

执行gnuplot绘图的shell文件如下:


#!/bin/bashgnuplot -persist<<EOFset terminal gifset output "cbr_delay.gif"set title "cbr_delay"set xlabel "simulation time"set ylabel "throughput/kbps"unset keyplot "cbr_delay" with linespointsEOF

上面那个
-persist<<EOF

能够阻止gnuplot弹出一堆东西,具体原因没有找到,找到了gnuplot的手册,但没看明白persist的意思手册解释如下:

To give gnuplot commands directly in the command line, using the "-persist" option so that the plot remains
on the screen afterwards:
gnuplot -persist -e "set title ’Sine curve’; plot sin(x)"

加persist之后,可以不用弹出一堆信息,即不会进入gnuplot的session

后面的<<EOF也不太清楚是什么意思?

cbr delay的图形



原创粉丝点击