资源描述
网络协议分析与仿真课程设计报告
网络模拟
一、 课程设计目的
l 掌握网络模拟工具NS2的使用,学习基本的网络模拟方法。
二、 课程设计内容
l 协议模拟
Ø 工具:NS2,awk,shell,perl等;
Ø 要求:掌握NS2网络模拟的基本流程;
Ø 内容:NS2网络模拟基本流程
编写TCL脚本,搭建如下图所示的一个网络,共6个节点,其中2、3节点用做ftp服务器和客户端,4、5节点用做cbr流量的源和目的,而0、1节点用做转发设备。各节点间的链路属性见图。
模拟时间设为13秒钟,在0.1秒开始产生cbr流量,在1.0秒开发发送发ftp流量;8.0秒ftp流量结束,12.0秒cbr流量结束。编写脚本(可用shell,awk,或perl等)分析模拟日志文件,统计每0.5s内0、1节点间链路通过的分组数以及字节数。
三、 设计与实现过程
1.仿真脚本代码与详细注解
#Create a simulator object #创建一个模拟器对象,赋值给变量ns
set ns [new Simulator]
#Define different colors for data flows (for NAM) #对不同的数据流定义
$ns color 1 Red #不同的颜色
$ns color 2 Yellow
#Open the NAM trace file #打开一个nam文件,存在则清空,无则新建
set nf [open out.nam w]
$ns namtrace-all $nf
#Open the Trace file #打开一个trace文件,方式同上。
set tf [open out.tr w] #用来记录封包传送的过程
$ns trace-all $tf
#Define a 'finish' procedure #定义一个finish过程
proc finish {} {
global ns nf tf
$ns flush-trace
close $nf #关闭nam文件
close $tf #关闭trace文件
exec nam out.nam & #以后台方式运行nam
exit 0
}
#Create six nodes #创建六个节点
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
#Create links between the nodes #根据条件创建节点之间的连接
$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n0 $n4 1.5Mb 10ms DropTail
$ns duplex-link $n1 $n3 1.5Mb 10ms DropTail
$ns duplex-link $n1 $n5 1.5Mb 10ms DropTail
$ns duplex-link $n1 $n0 2Mb 20ms DropTail
#Give node position (for NAM) #设置节点的位置,供nam使用
$ns duplex-link-op $n2 $n0 orient right-down
$ns duplex-link-op $n4 $n0 orient right-up
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n3 orient right-up
$ns duplex-link-op $n1 $n5 orient right-down
#Set Queue Size of link (n0-n1) to 10 #设置no和n1之间最大队列长度
$ns queue-limit $n1 $n0 10
#Setup a TCP connection #建立一条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的连接以蓝色表示
#Setup a UDP connection #建立一个UDP连接
set udp [new Agent/UDP]
$ns attach-agent $n4 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
set null [new Agent/Null]
$ns attach-agent $n5 $null
$ns connect $udp $null
$udp set fid_ 2 #在NAM中,UDP的连接以红色表示
#Setup a FTP over TCP connection #在TCP连接之上建立FTP应用
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a CBR over UDP connection #在UDP连接之上建立CBR应用
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 #设置FTP和CBR开始的时间
$ns at 0.1 "$cbr start" #在0.1秒开始产生cbr流量
$ns at 1.0 "$ftp start" #在0.1秒开始发送ftp流量
$ns at 8.0 "$ftp stop" #在8.0秒ftp流量结束
$ns at 12.0 "$cbr stop" #在12.0秒cbr流量结束
#Call the finish procedure after 13 seconds of simulation time
#在13秒调用finish过程结束模拟
$ns at 13.0 "finish"
#Run the simulation #执行模拟
$ns run
2.仿真过程示意(动画截图1个)
3.日志分析脚本设计(设计思路与代码、注解)
设计思路:首先按取出每行的时间、分组大小;若时间大于当前的时间段(intervals),令变量i+1,开始统计下一个时间段信息;数组count1记录分组的数目,它的下标与时间短的序号对应,数组count2记录每个时间段通过的分组的总字节数,它的下标也与时间段的序号对应。
脚本:
#!/usr/bin/perl
$i=0; #定义变量i
@info; #定义三个数组
@count1;
@count2;
while(<>){ #从标准输入读入数据
@info = split(/ +/,$_); #split函数以一个以上的空格为分隔符对每行数据进行分割。分割后的数据返回给info数组。
$reason = $info[0]; #封包事件发生的原因
$time = $info[1]; #info中的下标1对应的数据为时间
$src = $info[2]; #开始节点
$dst = $info[3]; #结束节点
$length = $info[5]; #info中下标5对应数据为分组大小
#if($reason==’r’ && ($src==0 && $dst==1) || ($src==1 && $dst==0)){
#封包进入队列,,且在0-1链路上,执行以下操作
if($time > $i*0.5){ #若时间大于当前的时间段,则开始统计下一时间段的信息
$i++;
}
$count1[$i]++; #对应时间段的分组数加1
$count2[$i]+=$length; #对应时间段的字节数累加
}
}
printf("intervals(s)\t\tpackets\t\tbytes\n");
$i=1.0;
while($count1[$i]){ #只要count1数组不为空就打印
printf("%4.1f~%4.1f\t\t$count1[$i]\t\t$count2[$i]\n",($i-1)*0.5,$i*0.5,);
$i++;
}
4.分析结果展示(通过自绘图来说明)
第一列:时间段(intervals),每0.5秒分成一个时间段;
第二列:分组数
第三列:通过分组的总字节数
绘制的折线统计图如下:
四、 设计技巧及体会
通过这次实验,我发现对于计算机网络还有很多地方不了解。在这次实验的过程中,我从中学到了很多。对于这次实验,关于NS2方面以及脚本方面还有很多不足,操作很不熟悉,不懂的地方也很多。而通过这次课设,不仅让我找出了我不足的地方,更让我学习了基本的网络模拟方法,掌握网络模拟工具NS2的使用以及脚本的设计。
展开阅读全文