基于NS2无线传感网“Ad Hoc网络仿真”设计 - 图文

更新时间:2023-10-18 23:32:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

课 程 设 计

课程名称:设计题目:学 院:专业年级:学 号:学生姓名:指导教师:日 期:

无线传感器网络原理及方法 基于NS2无线传感网“Ad Hoc网络仿真”设计 信息工程与自动化学院 2010级计算机系 104班

201010405424 李本双 江虹 2013-7月

课 程 设 计 任 务 书

信息工程与自动化 学院 计算机科学与技术 专业 2010 年级 学生姓名: 李本双

课程设计题目: 基于NS2无线传感网“Ad Hoc网络仿真”设计 课程设计主要内容:

本课程设计的任务是在原有书本知识和实验基础上学习嵌入式系统中基本的程序设计方法和技术。本课程设计的目的是通过设计使学生加深对理论课的理解,提高学生运用理论知识解决实际问题的能力。

基本理论

1、 无线传感网技术原理 2、 操作系统

3、 嵌入式软件开发技术 实验方法与基本要求

嵌入式开发采用宿主机加目标机的模式,宿主机是执行编译、链接、定址过程的计算机;目标机是远行嵌入式软件的硬件平台。要求学生掌握嵌入式系统开发的一般方法,整个设计过程必须由学生自己独立完成,课程设计按二人一组,分组进行全过程的设计。

设计内容

1、建立传感网汇聚节点

2、设计传感节点的网络结构(星形、树形)

3、设计上位机监控程序(图形方式显示节点拓扑结构、采集数据)

设 计 指 导 教 师 (签字): 教学基层组织负责人(签字):

2013年 7 月 13 日

- 2 -

目 录

1、课程设计的目的及要求??????????????4

1. 1、设计目的…………………………… ………………………………4 1. 2、所需仿真平台及网络仿真软件…… …………… … ………………4 1.3、ns-allinone-2.35的安装……………… …… ……………………4

2、WSN开发背景???????????? ????? 7

2.1、WSN简介?????????????? ????????7 2.2、 应用领域 ????????????? ?????????8 2.3、 主要特点 ???????????????? ??????8

2.4、 硬件平台????????????????? ?????8 2.5、软件平台??????????????? ???????8 2.6、仿真系统?????????????? ????????10 2.7、 数据可视化????????????? ????????10

3、 NS2开发理论及方法???????????????????10 4、Ad Hoc网络仿真通信场景的设计与实现??????????14

4.1 设计要求????????????? ????????14 4.2 详细设计???????????? ?????????14

随机建立通信场景文件???? ????????????????14 4.3 源代码(dd.tcl文件)??????? ?????????14

4.4 仿真截图????????????? ?????? ?????21

5、Ad Hoc网络仿真有关数据跟踪的设计与实现………………23

5.1 设计要求?????????????????????23 5.2 详细设计?????????????????????23 1)定义节点的配置参数????????? ????????23 2)初始化跟踪档案???????????? ???????24 5.3 源代码(aa.tcl文件)??? ???? ???????24 5.4 仿真截图???????? ????????? ??? 30 6.总结和体会??????????????????????33 7、参考文献?????????? ????????????33

- 3 -

基于NS2无线传感网“Ad Hoc网络仿真”设计

1、课程设计的目的及要求

1. 1、设计目的

1).学习Ad Hoc网络AODV路由协议与其在NS2中的仿真; 2).掌握NS2中的随机数据流与随机场景的生成; 3).初步学习使用trace文件的处理方法,进行。

4 ).建立个网络,再进行测试网络全程跟踪(对于吞吐量、数据包丢失、分组延迟)

5 ).评价指标为数据包成功递交率、平均延迟等

1. 2、所需仿真平台及网络仿真软件

Ubuntu系统13.04.1 ns-allinone-2.35

1.3、ns-allinone-2.35的安装 1).下载软件

ns-allinone-2.35.tar.gz (http://sourceforge.net/projects/nsnam/files/),将其放到你/home/administrator/桌面/ns2.35下(ns2.35是我的文件名,administrator是PC机我建立的用户名) 通过ctrl+alt+T打开终端,在其中输入 cd ns2.35 <回车>

tar zxvf ns-allinone-2.35.tar.gz <回车> 将ns2.35解压到当前目录下 2).安装前的准备工作

为了更好的安装ns2,需要更新ubuntu系统文件,在终端中输入命令 sudo apt-get update sudo apt-get upgrade

sudo apt-get dist-upgrade

首次采用sudo时,需要用户密码,也就是安装ubuntu时设置的。 还需要进行一些package的安装,输入命令 sudo apt-get install build-essential

sudo apt-get install tcl8.5 tcl8.5-dev tk8.5 tk8.5-dev sudo apt-get install libxmu-dev libxmu-headers

3).准备工作做好后,开始安装软件 $cd ns-allinone-2.35

$./install (别忘了前面的那个点)

4

安装过程需要一会。安装正确的话应该可以看到如下提示: IMPORTANT NOTICES:

4).配置环境变量

$sudo gedit .bashrc(这里一定要看清gedit和.bashrc之间有一个空格)

在弹出来的窗口的文件末尾加入:(注意:下列代码中wangtiantian/mysoft/是我的ns-allinone-2.35文件路径,要按照你自己的更改)

Export

PATH=$PATH:/home/administrato/桌面/ns2.35/bin:/home/administrator/桌 面/ns2.35/tcl8.5.10/unix:/home/administrator/桌面/ns2.35/tk8.5.10/unix export

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/administrator/桌面/ns2.35/otcl-1.14:/home/administrator/桌 面/ns2.35/lib

export

TCL_LIBRARY=$TCL_LIBRARY:/home/administrator/

桌面/ns2.35/tcl8.5.10/library

保存退出。 5).验证测试

重新打开一个终端,输入ns并回车 $ns

5

#

set udp_(2) [new Agent/UDP]

$ns attach-agent $node_(4) $udp_(2) set null_(2) [new Agent/Null]

$ns attach-agent $node_(6) $null_(2)

set cbr_(2) [new Application/Traffic/CBR] $cbr_(2) set packetSize_ 512 $cbr_(2) set interval_ 0.01 $cbr_(2) set random_ 1

$cbr_(2) set maxpkts_ 10000 $cbr_(2) attach-agent $udp_(2) $ns connect $udp_(2) $null_(2)

$ns at 6.96568928983328 \#

# 6 connecting to 7 at time 14.634230382570173 #

set udp_(3) [new Agent/UDP]

$ns attach-agent $node_(6) $udp_(3) set null_(3) [new Agent/Null]

$ns attach-agent $node_(7) $null_(3)

set cbr_(3) [new Application/Traffic/CBR] $cbr_(3) set packetSize_ 512 $cbr_(3) set interval_ 0.01 $cbr_(3) set random_ 1

$cbr_(3) set maxpkts_ 10000 $cbr_(3) attach-agent $udp_(3) $ns connect $udp_(3) $null_(3)

$ns at 2.634230382570173 \#

# 7 connecting to 8 at time 2.546173154165118 #

set udp_(4) [new Agent/UDP]

$ns attach-agent $node_(7) $udp_(4) set null_(4) [new Agent/Null]

$ns attach-agent $node_(8) $null_(4)

set cbr_(4) [new Application/Traffic/CBR] $cbr_(4) set packetSize_ 512 $cbr_(4) set interval_ 0.01 $cbr_(4) set random_ 1

$cbr_(4) set maxpkts_ 10000 $cbr_(4) attach-agent $udp_(4) $ns connect $udp_(4) $null_(4)

$ns at 2.546173154165118 \#

# 7 connecting to 9 at time 2.7030203154790309 #

set udp_(5) [new Agent/UDP]

$ns attach-agent $node_(7) $udp_(5) set null_(5) [new Agent/Null]

$ns attach-agent $node_(9) $null_(5)

set cbr_(5) [new Application/Traffic/CBR] $cbr_(5) set packetSize_ 512 $cbr_(5) set interval_ 0.01

16

$cbr_(5) set random_ 1

$cbr_(5) set maxpkts_ 10000 $cbr_(5) attach-agent $udp_(5) $ns connect $udp_(5) $null_(5)

$ns at 2.7030203154790309 \#

# 8 connecting to 9 at time 2.48548468411224 #

set udp_(6) [new Agent/UDP]

$ns attach-agent $node_(8) $udp_(6) set null_(6) [new Agent/Null]

$ns attach-agent $node_(9) $null_(6)

set cbr_(6) [new Application/Traffic/CBR] $cbr_(6) set packetSize_ 512 $cbr_(6) set interval_ 0.01 $cbr_(6) set random_ 1

$cbr_(6) set maxpkts_ 10000 $cbr_(6) attach-agent $udp_(6) $ns connect $udp_(6) $null_(6)

$ns at 2.48548468411224 \#

# 9 connecting to 10 at time 2.258212521792487 #

set udp_(7) [new Agent/UDP]

$ns attach-agent $node_(9) $udp_(7) set null_(7) [new Agent/Null]

$ns attach-agent $node_(10) $null_(7) set cbr_(7) [new Application/Traffic/CBR] $cbr_(7) set packetSize_ 512 $cbr_(7) set interval_ 0.01 $cbr_(7) set random_ 1

$cbr_(7) set maxpkts_ 10000 $cbr_(7) attach-agent $udp_(7) $ns connect $udp_(7) $null_(7)

$ns at 2.258212521792487 \#

# 9 connecting to 11 at time 4.464945688594575 #

set udp_(8) [new Agent/UDP]

$ns attach-agent $node_(9) $udp_(8) set null_(8) [new Agent/Null]

$ns attach-agent $node_(11) $null_(8) set cbr_(8) [new Application/Traffic/CBR] $cbr_(8) set packetSize_ 512 $cbr_(8) set interval_ 0.01 $cbr_(8) set random_ 1

$cbr_(8) set maxpkts_ 10000 $cbr_(8) attach-agent $udp_(8) $ns connect $udp_(8) $null_(8)

$ns at 4.464945688594575 \#

# 11 connecting to 12 at time 3.77338456491632 #

17

set udp_(9) [new Agent/UDP]

$ns attach-agent $node_(11) $udp_(9) set null_(9) [new Agent/Null]

$ns attach-agent $node_(12) $null_(9) set cbr_(9) [new Application/Traffic/CBR] $cbr_(9) set packetSize_ 512 $cbr_(9) set interval_ 0.01 $cbr_(9) set random_ 1

$cbr_(9) set maxpkts_ 10000 $cbr_(9) attach-agent $udp_(9) $ns connect $udp_(9) $null_(9)

$ns at 3.77338456491632 \#

# 11 connecting to 13 at time 1.455830739092008 #

set udp_(10) [new Agent/UDP]

$ns attach-agent $node_(11) $udp_(10) set null_(10) [new Agent/Null]

$ns attach-agent $node_(13) $null_(10) set cbr_(10) [new Application/Traffic/CBR] $cbr_(10) set packetSize_ 512 $cbr_(10) set interval_ 0.01 $cbr_(10) set random_ 1

$cbr_(10) set maxpkts_ 10000 $cbr_(10) attach-agent $udp_(10) $ns connect $udp_(10) $null_(10)

$ns at 1.455830739092008 \#

# 13 connecting to 14 at time 3.900868549896813 #

set udp_(11) [new Agent/UDP]

$ns attach-agent $node_(13) $udp_(11) set null_(11) [new Agent/Null]

$ns attach-agent $node_(14) $null_(11) set cbr_(11) [new Application/Traffic/CBR] $cbr_(11) set packetSize_ 512 $cbr_(11) set interval_ 0.01 $cbr_(11) set random_ 1

$cbr_(11) set maxpkts_ 10000 $cbr_(11) attach-agent $udp_(11) $ns connect $udp_(11) $null_(11)

$ns at 3.900868549896813 \#

# 14 connecting to 15 at time 2.17211061677529 #

set udp_(12) [new Agent/UDP]

$ns attach-agent $node_(14) $udp_(12) set null_(12) [new Agent/Null]

$ns attach-agent $node_(15) $null_(12) set cbr_(12) [new Application/Traffic/CBR] $cbr_(12) set packetSize_ 512 $cbr_(12) set interval_ 0.01 $cbr_(12) set random_ 1

18

$cbr_(12) set maxpkts_ 10000 $cbr_(12) attach-agent $udp_(12) $ns connect $udp_(12) $null_(12)

$ns at 2.17211061677529 \#

# 15 connecting to 16 at time 5.088702704333095 #

set udp_(13) [new Agent/UDP]

$ns attach-agent $node_(15) $udp_(13) set null_(13) [new Agent/Null]

$ns attach-agent $node_(16) $null_(13) set cbr_(13) [new Application/Traffic/CBR] $cbr_(13) set packetSize_ 512 $cbr_(13) set interval_ 0.01 $cbr_(13) set random_ 1

$cbr_(13) set maxpkts_ 10000 $cbr_(13) attach-agent $udp_(13) $ns connect $udp_(13) $null_(13)

$ns at 5.088702704333095 \#

# 15 connecting to 17 at time 4.420613009212822 #

set udp_(14) [new Agent/UDP]

$ns attach-agent $node_(15) $udp_(14) set null_(14) [new Agent/Null]

$ns attach-agent $node_(17) $null_(14) set cbr_(14) [new Application/Traffic/CBR] $cbr_(14) set packetSize_ 512 $cbr_(14) set interval_ 0.01 $cbr_(14) set random_ 1

$cbr_(14) set maxpkts_ 10000 $cbr_(14) attach-agent $udp_(14) $ns connect $udp_(14) $null_(14)

$ns at 4.420613009212822 \#

# 16 connecting to 17 at time 3.92280978985261 #

set udp_(15) [new Agent/UDP]

$ns attach-agent $node_(16) $udp_(15) set null_(15) [new Agent/Null]

$ns attach-agent $node_(17) $null_(15) set cbr_(15) [new Application/Traffic/CBR] $cbr_(15) set packetSize_ 512 $cbr_(15) set interval_ 0.01 $cbr_(15) set random_ 1

$cbr_(15) set maxpkts_ 10000 $cbr_(15) attach-agent $udp_(15) $ns connect $udp_(15) $null_(15)

$ns at 3.92280978985261 \#

# 16 connecting to 18 at time 2.20174070317378 #

set udp_(16) [new Agent/UDP]

19

$ns attach-agent $node_(16) $udp_(16) set null_(16) [new Agent/Null]

$ns attach-agent $node_(18) $null_(16) set cbr_(16) [new Application/Traffic/CBR] $cbr_(16) set packetSize_ 512 $cbr_(16) set interval_ 0.01 $cbr_(16) set random_ 1

$cbr_(16) set maxpkts_ 10000 $cbr_(16) attach-agent $udp_(16) $ns connect $udp_(16) $null_(16)

$ns at 2.20174070317378 \#

# 17 connecting to 18 at time 1.99343390995331 #

set udp_(17) [new Agent/UDP]

$ns attach-agent $node_(17) $udp_(17) set null_(17) [new Agent/Null]

$ns attach-agent $node_(18) $null_(17) set cbr_(17) [new Application/Traffic/CBR] $cbr_(17) set packetSize_ 512 $cbr_(17) set interval_ 0.01 $cbr_(17) set random_ 1

$cbr_(17) set maxpkts_ 10000 $cbr_(17) attach-agent $udp_(17) $ns connect $udp_(17) $null_(17)

$ns at 1.99343390995331 \#

#Total sources/connections: 12/18 #

$ns rtmodel-at 1.0 down $node_(1) $node_(2) $ns rtmodel-at 2.0 up $node_(1) $node_(2)

#在仿真环境中,5秒后去呼叫finish来结束仿真(这样要注意仿真环境中#的5秒并不一定等于实际仿真的时间 $ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \$ns at 29.5 \

$ns at 30.0 \

20

吞吐量跟踪截图

31

数据包丢失跟踪截图

分组延迟跟踪截图

32

运行后的文件夹截图

6、总结与体会

经过本次课程设计实验中,遇到了很多问题同时也学到了好多东西,由于我安装的是winxp系统,因此刚开始的时候就尝试用cygwin (setup.exe)弄了半天安好了总是遇到许多未知错误,因为ns2是在LINUX系统下开发的。因此我决定再装一个系统(ubuntu系统)来完成本次课程设计。我下载了一个ubuntu13.04版本,由于要装双系统所以比较麻烦,我是采用在线安装的方式进行,安装6G的用了大约3个小时时间,基本上装个双系统就弄了一天的时间,在周二开始下载安装 ns-allinone-2.35 。在上学期的课程中我们虽热接触过无线传感器网络的知识,可那都是基础并没有安排实验课程进行过实践,所以一切都得靠我自己去学习,通过网络上的资源也使我养成了自学的能力,从安装到应用总的所用时间就过去了三天。慢慢的我就先看了ns2.35的具体简单介绍之后再结合者里面的例子,一步步的来实现自己想要的东西。刚开始很是困难总是报错,在此过程中也和相应的老师沟通过,可最终的问题还是自己解决的,也多少有些自我成就感的东西,仿佛找到了很久以前的自己,那个一心融入学习的自我。总之,这次自我感受颇多,我所追求的是什么,人家需要什么,我又能做什么,我需要怎么做很是矛盾?有的时候现实往往是和理想对立的,梦想真的就只是个遥不可及的梦,并不是我不努力而是???

只希望我能赶快弄明白并掌握好自己的命运吧!

7、参考文献

[1]马崇霄,吴长奇.基于网络仿真器NS2的Adhoc网络路由协议仿真[J].电子测量技

术,2008(5):75-79

33

#执行仿真 $ns run

4.4 仿真截图

随机自动生成的场景

21

相关文件截图

22

仿真后的窗口

5、Ad Hoc网络仿真有关数据跟踪的设计与实现 5.1 设计要求

1) 先建立个网络,再进行测试网络全程跟踪(对于吞吐量、数据包丢失、分组延迟) 2) 每次实验时间不少于80s

3) 评价指标为数据包成功递交率、平均延迟等 5.2 详细设计

1)定义节点的配置参数

#===================================================================

set val(chan) Channel/WirelessChannel ;# channel type 通道类型 set val(prop) Propagation/TwoRayGround ;# 无线传播模型 set val(netif) Phy/WirelessPhy ;# 网络接口类型

23

set val(mac) Mac/802_11 ;# MAC type MAC类型

set val(ifq) Queue/DropTail/PriQueue ;# interface queue type 接口队列类型 set val(ll) LL ;# link layer type 链路层类型 set val(ant) Antenna/OmniAntenna ;# antenna model 天线模型

set val(ifqlen) 50 ;# max packet in ifq IFQ最大包 set val(nn) 8 ;#移动节点数量

set val(rp) DSDV ;# routing protocol 路由协定 set val(x) 500 ;# X尺寸的地形 set val(y) 500 ;# Y尺寸的地形 Mac/802_11 set RTSThreshold_ 3000 Mac/802_11 set basicRate_ 1Mb Mac/802_11 set dataRate_ 2Mb

2)初始化跟踪档案

#===================================================================== # *** 吞吐量跟踪 *** set f0 [open out02.tr w] set f1 [open out12.tr w] set f2 [open out22.tr w] set f3 [open out32.tr w] # *** 数据包丢失跟踪 *** set f4 [open lost02.tr w] set f5 [open lost12.tr w] set f6 [open lost22.tr w] set f7 [open lost32.tr w] # *** 分组延迟跟踪 *** set f8 [open delay02.tr w] set f9 [open delay12.tr w] set f10 [open delay22.tr w] set f11 [open delay32.tr w]

用图记录的统计

exec xgraph out02.tr out12.tr out22.tr out32.tr -geometry 800x400 & exec xgraph lost02.tr lost12.tr lost22.tr lost32.tr -geometry 800x400 & exec xgraph delay02.tr delay12.tr delay22.tr delay32.tr -geometry 800x400 &

5.3 源代码(aa.tcl)

#=================================================================== # Define Node Configuration paramaters定义节点的配置参数

#===================================================================

set val(chan) Channel/WirelessChannel ;# channel type 通道类型

set val(prop) Propagation/TwoRayGround ;# radio-propagation model 无线传播模型 set val(netif) Phy/WirelessPhy ;# network interface type 网络接口类型 set val(mac) Mac/802_11 ;# MAC type MAC类型

set val(ifq) Queue/DropTail/PriQueue ;# interface queue type 接口队列类型 set val(ll) LL ;# link layer type 链路层类型 set val(ant) Antenna/OmniAntenna ;# antenna model 天线模型

set val(ifqlen) 50 ;# max packet in ifq IFQ最大包

set val(nn) 8 ;# number of mobilenodes 移动节点数量 set val(rp) DSDV ;# routing protocol 路由协定

set val(x) 500 ;# X dimension of the topography X尺寸的地形 set val(y) 500 ;# Y dimension of the topography Y尺寸的地形 Mac/802_11 set RTSThreshold_ 3000 Mac/802_11 set basicRate_ 1Mb Mac/802_11 set dataRate_ 2Mb

#===================================================================== # 初始化跟踪档案desctiptors“

24

#===================================================================== # *** 吞吐量跟踪 *** set f0 [open out02.tr w] set f1 [open out12.tr w] set f2 [open out22.tr w] set f3 [open out32.tr w] # *** 数据包丢失跟踪 *** set f4 [open lost02.tr w] set f5 [open lost12.tr w] set f6 [open lost22.tr w] set f7 [open lost32.tr w] # *** 分组延迟跟踪 *** set f8 [open delay02.tr w] set f9 [open delay12.tr w] set f10 [open delay22.tr w] set f11 [open delay32.tr w]

#以下部分的代码,在 入门实例1 中已有详尽的注释啦! set ns_ [new Simulator]

set tracefd [open trace2.tr w] $ns_ trace-all $tracefd

set namtrace [open sim12.nam w]

$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)

set topo [new Topography] $topo load_flatgrid 500 500

# 建立常规运营总监(GOD)对象。

#它是用来存储全局信息的环境状况,网络,或节点的 #观察员将存在,但不会参与模拟的。

create-god $val(nn) # 配置节点

$ns_ node-config -adhocRouting $val(rp) \\ -llType $val(ll) \\ -macType $val(mac) \\ -ifqType $val(ifq) \\ -ifqLen $val(ifqlen) \\ -antType $val(ant) \\ -propType $val(prop) \\ -phyType $val(netif) \\ -channelType $val(chan) \\ -topoInstance $topo \\ -agentTrace ON \\ -routerTrace ON \\ -macTrace OFF \\

-movementTrace OFF

# 创建节点

for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node]

$node_($i) random-motion 0 ;# 禁用随机运动 }

# 初始化节点坐标

$node_(0) set X_ 5.0

25

$node_(0) set Y_ 5.0 $node_(0) set Z_ 0.0

$node_(1) set X_ 200.0 $node_(1) set Y_ 5.0 $node_(1) set Z_ 0.0

$node_(2) set X_ 5.0 $node_(2) set Y_ 50.0 $node_(2) set Z_ 0.0

$node_(3) set X_ 200.0 $node_(3) set Y_ 50.0 $node_(3) set Z_ 0.0

$node_(4) set X_ 5.0 $node_(4) set Y_ 100.0 $node_(4) set Z_ 0.0

$node_(5) set X_ 200.0 $node_(5) set Y_ 100.0 $node_(5) set Z_ 0.0

$node_(6) set X_ 2.0 $node_(6) set Y_ 150.0 $node_(6) set Z_ 0.0

$node_(7) set X_ 200.0 $node_(7) set Y_ 150.0 $node_(7) set Z_ 0.0

# 设置节点之间的流量

# TCP连接之间node_(0) and node_(1) # 创建恒四个比特率的流量来源

#============================================================ #以下的这段代码可以写成一个过程,那样就方便多啦! #先整理, 以后我再回头改吧!

#============================================================

set agent1 [new Agent/UDP] ;# Create TCP Agent创建TCP代理 $agent1 set prio_ 0 ;# 设置其优先级为0 # 创建亏损监视器片,为了能够跟踪数字obytes的

set sink [new Agent/LossMonitor]

$ns_ attach-agent $node_(0) $agent1 ;# 源节点将代理 $ns_ attach-agent $node_(1) $sink ;# 附加剂到汇聚节点 $ns_ connect $agent1 $sink ;# 连接节点

set app1 [new Application/Traffic/CBR] ;# 创建恒定比特率的应用 $app1 set packetSize_ 512 ;# 设置数据包大小为512字节 $app1 set rate_ 600Kb ;# 集的CBR率为200比特/秒 $app1 attach-agent $agent1 ;# 附加应用程序代理

set agent2 [new Agent/UDP] ;# 创建TCP代理 $agent2 set prio_ 1 ;# 设置优先级为1 # 创建为损耗监测接收器能够接收道数obytes set sink2 [new Agent/LossMonitor]

26

$ns_ attach-agent $node_(2) $agent2 ;# 附加剂对源节点 $ns_ attach-agent $node_(3) $sink2 ;# 附加剂到汇聚节点 $ns_ connect $agent2 $sink2 ;# Connect the nodes set app2 [new Application/Traffic/CBR] ;# 创建恒定比特率的应用 $app2 set packetSize_ 512 ;# 设置数据包大小为512字节 $app2 set rate_ 600Kb ;# 集的CBR率为200比特/秒 $app2 attach-agent $agent2 ;# 附加应用程序代理

set agent3 [new Agent/UDP] ;# 创建TCP代理 $agent3 set prio_ 2 ;# 设置优先级为2 set sink3 [new Agent/LossMonitor]

$ns_ attach-agent $node_(4) $agent3 ;# 附加剂对源节点 $ns_ attach-agent $node_(5) $sink3 ;# 附加剂到汇聚节点 $ns_ connect $agent3 $sink3 ;# Connect the nodes set app3 [new Application/Traffic/CBR] ;# 创建恒定比特率的应用 $app3 set packetSize_ 512 ;# 设置数据包大小为512字节 $app3 set rate_ 600Kb ;# 集的CBR率为200比特/秒 $app3 attach-agent $agent3 ;# 附加应用程序代理

set agent4 [new Agent/UDP] ;# 创建TCP代理 $agent4 set prio_ 3 ;# 设置优先级为3 # 创建为损耗监测接收器能够接收道数obytes

set sink4 [new Agent/LossMonitor] ;#同上(包括以下) $ns_ attach-agent $node_(6) $agent4 ;# 附加剂对源节点 $ns_ attach-agent $node_(7) $sink4 ;# 附加剂到汇聚节点 $ns_ connect $agent4 $sink4 ;# Connect the nodes set app4 [new Application/Traffic/CBR] ;# 创建恒定比特率的应用

$app4 set packetSize_ 512 ;# S设置数据包大小为512字节 $app4 set rate_ 600Kb ;# 集的CBR率为200比特/秒 $app4 attach-agent $agent4 ;# 附加应用程序代理

# 定义了网络动画的节点规模

for {set i 0} {$i < $val(nn)} {incr i} { $ns_ initial_node_pos $node_($i) 20 }

# Initialize Flags初始化 set holdtime 0 set holdseq 0

set holdtime1 0 set holdseq1 0

set holdtime2 0 set holdseq2 0

set holdtime3 0 set holdseq3 0

set holdrate1 0 set holdrate2 0 set holdrate3 0 set holdrate4 0

# 函数来记录statistcis(比特率,延迟,损失)

27

proc record {} {

global sink sink2 sink3 sink4 f0 f1 f2 f3 f4 f5 f6 f7 holdtime holdseq holdtime1 holdseq1 holdtime2 holdseq2 holdtime3 holdseq3 f8 f9 f10 f11 holdrate1 holdrate2 holdrate3 holdrate4 set ns [Simulator instance]

set time 0.9 ;#Set Sampling Time to 0.9 Sec set bw0 [$sink set bytes_] set bw1 [$sink2 set bytes_] set bw2 [$sink3 set bytes_] set bw3 [$sink4 set bytes_] set bw4 [$sink set nlost_] set bw5 [$sink2 set nlost_] set bw6 [$sink3 set nlost_] set bw7 [$sink4 set nlost_]

set bw8 [$sink set lastPktTime_] set bw9 [$sink set npkts_]

set bw10 [$sink2 set lastPktTime_] set bw11 [$sink2 set npkts_]

set bw12 [$sink3 set lastPktTime_] set bw13 [$sink3 set npkts_]

set bw14 [$sink4 set lastPktTime_] set bw15 [$sink4 set npkts_]

set now [$ns now]

# 跟踪文件中记录的比特率

puts $f0 \puts $f1 \puts $f2 \puts $f3 \

# 文件记录的数据包丢失率

puts $f4 \puts $f5 \puts $f6 \puts $f7 \

# 文件记录分组延迟

if { $bw9 > $holdseq } {

puts $f8 \} else {

puts $f8 \}

if { $bw11 > $holdseq1 } {

puts $f9 \} else {

puts $f9 \}

if { $bw13 > $holdseq2 } {

puts $f10 \} else {

puts $f10 \}

if { $bw15 > $holdseq3 } {

28

puts $f11 \} else {

puts $f11 \}

# 重置变量

$sink set bytes_ 0 $sink2 set bytes_ 0 $sink3 set bytes_ 0 $sink4 set bytes_ 0

$sink set nlost_ 0 $sink2 set nlost_ 0 $sink3 set nlost_ 0 $sink4 set nlost_ 0

set holdtime $bw8 set holdseq $bw9 set holdrate1 $bw0 set holdrate2 $bw1 set holdrate3 $bw2 set holdrate4 $bw3

$ns at [expr $now+$time] \定时采样的设置! }

# 设置模拟事件!

$ns_ at 0.0 \

$ns_ at 1.4 \$ns_ at 10.0 \$ns_ at 20.0 \$ns_ at 30.0 \

$ns_ at 80.0 \

# Reset Nodes at time 80 sec

for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at 80.0 \}

# 退出仿真时间在80.01秒时

$ns_ at 80.01 \proc stop {} {

global ns_ tracefd f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 # 关闭跟踪文件 close $f0 close $f1 close $f2 close $f3 close $f4 close $f5 close $f6 close $f7 close $f8 close $f9 close $f10

29

close $f11

# 用图记录的统计

exec xgraph out02.tr out12.tr out22.tr out32.tr -geometry 800x400 &

exec xgraph lost02.tr lost12.tr lost22.tr lost32.tr -geometry 800x400 & exec xgraph delay02.tr delay12.tr delay22.tr delay32.tr -geometry 800x400 & # 重置跟踪文件 $ns_ flush-trace close $tracefd exit 0 }

puts \$ns_ run

5.4 仿真截图

运行截图

30

本文来源:https://www.bwwdw.com/article/zbxf.html

Top