当前位置:
文档之家› 网络协议仿真(By月西RSing)
网络协议仿真(By月西RSing)
$count=0; $cbr=0; $tcp=0; } else { $pkt+=$size[$j]; $count++; if($type[$j] eq cbr){ $cbr++; $scbr++; } if($type[$j] eq tcp){ $tcp++; $stcp++; } } } $per=($lost/$i)*100; $per=sprintf"%.1f",$per; print "\n"; print "Total Pkts:\t",$total,"\n"; print "Total Count:\t",$i,"\n"; print "Total Tcp:\t",$stcp,"\n"; print "Total Cbr:\t",$scbr,"\n"; print "Lost Pkts:\t",$lost,"\n"; print "Lost Percent:\t",$per,"%\n\n";
#建立一条 TCP 连接 set tcp [new Agent/TCP] #$tcp set class_ 2 $ns attach-agent $n2 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink $ns connect $tcp $sink $tcp set fid_ 1 #在 NAM 中 TCP 连接用蓝色表示 #建立一个 UDP 连接 set udp [new Agent/UDP] $ns attach-agent $n4 $udp set null [new Agent/Null] $ns attach-agent $n5 $null $ns connect $udp $null $udp set fid_ 2 #定义 UDP 资料流使用红色标识 #在 TCP 连接之上建立 FTP 连接 set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP #在 UDP 之上建立 CBRl 连接 set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1024 $cbr set rate_ 1mb $cbr set random_ false #定义事件的发生时间和结束时间 $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 8.0 "$ftp stop" $ns at 12.0 "$cbr stop" #在 13 秒之后呼叫 finish 函数来结束模拟 $ns at 13.0 "finish" #执行模拟 $ns run
ftp
2 3
ftp sink
1.5mbps, 10ms 2mbps, 20ms
0 1
1.5mbps, 10ms
1.5mbps, 10ms
4
1.5mbps, 10ms
5
cbr
packet size = 1kbytes, rate=1Mbps
null
模拟时间设为 13 秒钟,在 0.1 秒开始产生 cbr 流量,在 1.0 秒开发发送发 ftp 流量;8.0 秒 ftp 流量结束,12.0 秒 cbr 流量结束。编写脚本(可用 shell, awk,或 perl 等)分析模拟日志文件,统计每 0.5s 内 0、1 节点间链路通过的 分组数以及字节数。 三、 设计与实现过程
1.仿真脚本代码与详细注解 #创建一个仿真对象 set ns [new Simulator] #为不同的资料流定义不同的颜色 $ns color 1 Blue #将第一类资料流定义为蓝色 $ns color 2 Red #将第二类资料流定义为红色
#开启一个 NAM trace file $ns use-newtrace set nf [open out.nam w] $ns namtrace-all $nf #开启一个 trace file,用来记录封包的传送过程 set tf [open out.tr w] $ns trace-all $tf #定义一个结束程序 proc finish {} { global ns nf tf $ns flush-trace close $nf close $tf exec nam out.nam & exit 0 }
西安郵電大學 (By月西RSing)
网络协议分析与仿真 课程设计报告书
院系名称 : 实验内容 : 学生姓名 : 专业名称 : 班 学 时 级 : 号 : 间 :
网络协议分析与仿真课程设计报告 网络模拟
一、 课程设计目的 掌握网络模拟工具 NS2 的使用,学习基本的网AM
#产生实验所需要的六个网络节点 set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] #把节点连接起来,并且设定相互之间的传输速率和时延 $ns duplex-link $n0 $n1 2Mb 20ms DropTail $ns duplex-link $n1 $n3 1.5Mb 10ms DropTail $ns duplex-link $n1 $n5 1.5Mb 10ms DropTail $ns duplex-link $n0 $n2 1.5Mb 10ms DropTail $ns duplex-link $n0 $n4 1.5Mb 10ms DropTail #定义 n0 到 n1 之间的队列限制为十个封包大小 $ns queue-limit $n0 $n1 10 #定义节点的位置,前者是后者的参照,供 NAM 使用 $ns duplex-link-op $n1 $n0 orient left $ns duplex-link-op $n1 $n3 orient right-up $ns duplex-link-op $n1 $n5 orient right-down $ns duplex-link-op $n0 $n2 orient left-up $ns duplex-link-op $n0 $n4 orient left-down
协议模拟
工具:NS2,awk,shell,perl 等; 要求:掌握 NS2 网络模拟的基本流程; 内容:NS2 网络模拟基本流程 编写 TCL 脚本,搭建如下图所示的一个网络,共 6 个节点,其中 2、3 节 点用做 ftp 服务器和客户端,4、5 节点用做 cbr 流量的源和目的,而 0、1 节 点用做转发设备。各节点间的链路属性见图。
Perl 源码 #!/usr/bin/perl $i=0; $lost; @time; @size; @type; while(<>){ @words=split(/ +/,$_); if(($words[2]==0 and $words[3]==1) or ($words[2]==1 and $words[3]==0)) { if($words[0] eq r) { $i++; $time[$i]=$words[1]; $size[$i]=$words[5]; $type[$i]=$words[4]; } if($words[0] eq d) { $lost++; } } } $now=0; $pkt=0; $cbr=0; $tcp=0; $count=0; $total=0; $scbr=0; $stcp=0; print "\n\n\n\t The Count of The Pkts \n\n"; print "Start\tEnd\tCounts\tPkts\tTcp\tCbr\n"; for($j=1;$j<=$i;$j++) { $total+=$size[$j]; if($time[$j]-$now>=0.5) { print $now,"\t",$now+0.5,"\t",$count,"\t",$pkt,"\t",$tcp,"\t",$cbr,"\n"; $now+=0.5; $pkt=0;
2.仿真过程示意(动画截图 1 个)
(1)发生拥塞时候的截图
(2)TCP 传输时的截图
3.日志分析脚本设计(设计思路与代码、注解)
4.分析结果展示(通过自绘图来说明) (1)数据包大小的曲线图
(2)数据包个数的曲线图
(3)统计结果
四、
设计技巧及体会
1.在这次试验中,掌握了网络模拟工具 NS2的使用,学习了基本的网络模拟 方法,但是,对于仿真脚本代码中的有些语句了解的还不是很透彻,是照葫芦画 瓢的方式写出来的,在以后,一定要对这些东西加深理解,这样设计起来才能够 更加的轻松,还有就是使用 perl 写程序的时候,由于思路不清楚,开始写的程 序有很多的漏洞,经过一次次的加深理解,最终将程序改进。 2.在设计的过程中,发现了自己对 shell 了解的很少,最后通过 perl 语言 写了日志脚本,以后要多学习一下 shell 脚本,这是一个很优秀的语言,尽量在 下次能够用 shell 写出属于自己的程序。 (后面附程序源代码)