XUPT大学
实验名称: 隐藏节点问题仿真
专 业: 网络工程 班 级: 1703
姓 名: 阿吉
学 号:
指导教师:
日 期: 2020年
一.实验目的
掌握无线网络中隐藏节点问题的本质
利用 NS2 分析隐藏节点问题
二.实验环境 隐藏节点的实验分析采用了实验一搭建的实验平台,NS2 的版本为 2.35 三.实验内容
4.1.1 实验代码 #仿真模块参数设置
Mac/802_11 set RTSThreshold_ 0.......................................................................................1 Antenna/OmniAntenna set X_ 0.......................................................................................2 Antenna/OmniAntenna set Y_ 0.......................................................................................3 Antenna/OmniAntenna set Z_ 1.5....................................................................................4 Antenna/OmniAntenna set Gt_ 1.0.....................................................................................5 Antenna/OmniAntenna set Gr_ 1.0.....................................................................................6 Phy/WirelessPhy set CPThresh_ 10.0...................................................................................7 Phy/WirelessPhy set CSThresh_ 8.91754e-10..........................................................................8 Phy/WirelessPhy set RXThresh_ 2.81838e-9...........................................................................9 Phy/WirelessPhy set bandwidth_ 2e6..................................................................................10 Phy/WirelessPhy set Pt_ 0.281838............................................................................11 Phy/WirelessPhy set freq_ 9.14e+6.............................................................................12 Phy/WirelessPhy set L_ 1.0....................................................................................13
#仿真变量设置
set val(chan) Channel/WirelessChannel......................................................................14 set val(prop) Propagation/TwoRayGround..................................................................15 set val(netif) Phy/WirelessPhy................................................................................16 set val(mac) Mac/802_11......................................................................................17 set val(ifq) Queue/DropTail/PriQueue.....................................................................18 set val(ll) LL..................................................................................................19
set val(ant) Antenna/OmniAntenna..........................................................................20 set val(ifqlen) 100................................................................................................21 set val(rp) DSDV..............................................................................................22
#ns实例化和trace文件设置
set ns [new Simulator] .............................................................................................................23 set f [open Hidden_Terminal.tr w]..............................................................................................24 $ns trace-all $f.....................................................................................................................25 $ns eventtrace-all..................................................................................................................26 set nf [open Hidden_Terminal.nam w]..........................................................................................27 $ns namtrace-all-wireless $nf 500 500..........................................................................................28
#仿真拓扑设置
set topo [new Topography].....................................................................................................29 $topo load_flatgrid 500 500......................................................................................................30 create-god 3........................................................................................................................31
#节点配置
set chan [new $val(chan)].........................................................................................................32
$ns node-config -adhocRouting $val(rp) \................................................................................................................33 -llType $val(ll) \...........................................................................................................................34
-macType $val(mac) \..................................................................................................................35
-ifqType $val(ifq) \......................................................................................................................36
-ifqLen $val(ifqlen) \.....................................................................................................................37 -antType $val(ant) \.......................................................................................................................38 -propType $val(prop) \..................................................................................................................39 -phyType $val(netif) \...................................................................................................................40 -channel $chan \............................................................................................................................41 -topoInstance $topo \....................................................................................................................42 -agentTrace ON \..........................................................................................................................43 -routerTrace OFF \.......................................................................................................................44 -macTrace ON \...........................................................................................................................45 -movementTrace OFF.....................................................................................46
#节点实例化
for {set i 0} {$i < 3} {incr i} {...................................................................................................47
set node_($i) [$ns node].................................................................................................48 $node_($i) random-motion 0..............................................................................................49 }......................................................................................................................................50
#节点位置设置
$node_(0) set X_ 30.0................................................................................................................51 $node_(0) set Y_ 130.0...........................................................................................................52 $node_(0) set Z_ 0.0...............................................................................................................53 $node_(1) set X_ 180.0...........................................................................................................54 $node_(1) set Y_ 130.0...........................................................................................................55 $node_(1) set Z_ 0.0...............................................................................................................56 $node_(2) set X_ 330.0...........................................................................................................57 $node_(2) set Y_ 130.0...........................................................................................................58 $node_(2) set Z_ 0.0...............................................................................................................59
#代理和数据流设置
set udp [new Agent/mUDP] .....................................................................................................60
$udp set_filename sd1.............................................................................................................61 $ns attach-agent $node_(0) $udp............... ..................................................................................62
set null [new Agent/mUdpSink] .................................................................................................63 $null set_filename rd1.............................................................................................................64 $ns attach-agent $node_(1) $null................................................................................................65 $ns connect $udp $null............................................................................................................66 set cbr [new Application/Traffic/CBR] ........................................................................................67 $cbr attach-agent $udp............................................................................................................68 $cbr set type_ CBR................................................................................................................69 $cbr set packet_size_ 1000.......................................................................................................70 $cbr set rate_ 1Mb.................................................................................................................71 $cbr set random_ false.............................................................................................................72 $ns at 1.5 "$cbr start"..............................................................................................................73 $ns at 15.0 "$cbr stop".............................. ...............................................................................74
set udp2 [new Agent/mUDP] ....................................................................................................75 $udp2 set_filename sd2...........................................................................................................76 $ns attach-agent $node_(2) $udp2..............................................................................................77 set null2 [new Agent/mUdpSink] ...............................................................................................78 $null2 set_filename rd2...........................................................................................................79 $ns attach-agent $node_(1) $null2..............................................................................................80 $ns connect $udp2 $null2.........................................................................................................81 set cbr2 [new Application/Traffic/CBR] .......................................................................................82 $cbr2 attach-agent $udp2.........................................................................................................83 $cbr2 set type_ CBR...............................................................................................................84 $cbr2 set packet_size_ 1000......................................................................................................85 $cbr2 set rate_ 1Mb................................................................................................................86 $cbr2 set random_ false...........................................................................................................87 $ns at 2.0 "$cbr2 start"............................................................................................................88 $ns at 15.0 "$cbr2 stop"............................................................................ ...............................89
#节点初始化
for {set i 0} {$i < 3} {incr i} {..................................................................................................90 $ns initial_node_pos $node_($i) 30......................................................................................91
$ns at 20.0 "$node_($i) reset"............................................................................................92 }......................................................................................................................................93
#仿真完成处理函数
proc finish {} {.....................................................................................................................94 global ns f nf val.............................................................................................................95
$ns flush-trace..............................................................................................................96
close $f.......................................................................................................................97
close $nf......................................................................................................................98
exec nam Hidden_Terminal.nam &......................................................................................99
exit 0.........................................................................................................................100 }.....................................................................................................................................101
#仿真结束
$ns at 20.0 "finish"...............................................................................................................102 $ns at 20.1 "puts \"NS EXITING...\"; $ns halt"..............................................................................103
#启动仿真
$ns run.............................................................................................................................104
四.实验过程及分析
(1) 完成实验一后,在“home/<用户名>/WNT”(Ubuntu 中“<用户名>”为 WNT,Cygwin 中“<用户名>”为当前 Windows 系统的用户名)目录下。
(2) 打开终端(Ubuntu 为命令“Ctrl+Alt+T”,Cygwin 则需要双击
并在命令行输入命 令“startxwin.bat”),输入“cd WNT/experiment/exp2”命令进入本实验脚本 所在文件 夹。输入“ns Hidden_Terminal.tcl”命令,运行实验。
(3) 利用 NAM 动画观察实验结果,如图 2.3 所示。注意,在观看 NAM 动画时,运 行的步⻓调整为 2ms 即可,不宜过大,以免无法观察到详细的仿真过程。
第 1~13 行,对仿真中需用到的模块参数进行设置,需注意,此处并未给出所有参数的 设置,仅对必要的参数进行修改,若未给出则意味采用 NS2 中的默认值;
第 14~22 行,对仿真中需要用到的变量进行设置,该部分代码仅为便于修改代码(多处
采用相同模块时仅需修改变量即可),并未增加任何功能;
第 23~28 行,实例化 ns 变量,同时设置仿真过程中的 trace 和 nam 输出文件,以便对 实验结果进行分析和演示;
第 29~31 行,对仿真实验的拓扑范围和全局节点记录进行设置,以便控制节点位置和运 动范围;
第 32~46 行,在节点实例化前对参数进行配置,如路由协议、天线模型、物理层模块等, 实现正确有效的隐藏节点实验分析;
第 47~50 行,对节点进行实例化,所有节点具有相同配置,如需更改节点配置,需在实 例化节点前重新用 node-config 进行配置;
第 51~59 行,对实例化的节点进行位置的设定,注意,位置不能超过拓扑范围;
第 60~89 行,设置两条传输层为 mUDP 的数据流,应用层都为 CBR,其中第 1 条流从 节点 0 到节点 1,速率为 1Mbps,数据包大小为 1000 字节,仿真起止时间为 1.5s~15.0s; 第2 条流从节点 2 到节点 1,速率和数据包大小同第 1 条流,仿真起止时间为 2.0s~15.0s。 节点1为两条流的共同目标节点,此时将会出现隐藏节点问题;
第 90~93 行,对节点大小进行设置,用于 nam 演示,同时为实验结束时的节点进行 reset 事件设置,本实验的实验结束时间为 20.0s;
第 94~101 行,对仿真结束时的后续处理函数(finish)功能定义,主要有关闭打开的文件、 启动演示动画等,读者可在其基础上自行扩展;
第 102~103 行,仿真结束事件的设置,此处调用了之前定义的 finish 函数,同时输出提 示信息,表明仿真实验的结束并停机;
第 104 行,启动 ns 运行仿真。
五.问题回答 无线局域网隐藏节点问题对网络传输影响较大,极大降低了网络吞吐量,增大了时延,
而 CTS/RTS 机制则较好的解决隐藏节点问题。 仿真结果可从两个方面来分析,即仿真动画和仿真数据。在 NS2 中演示仿真动画采用 NAM,它将网络仿真过程中拓扑结构、数据包传输情况及队列丢包等以动画形式展现,本 实验仿真动画为电子资源中实验动画文件夹的对应文件。仿真结束后,可通过分析产生的 trace 数据对仿真过程中的延迟、丢包、吞吐量等进行分析。
六.总结
1.实验过程中遇到的问题及解决办法;
无线局域网隐藏节点问题对网络传输影响较大,极大降低了网络吞吐量,增 大了时延, 而 CTS/RTS 机制则较好的解决隐藏节点问题。 仿真结果可从两个 方面来分析,即仿真动画和仿真数据。在 NS2 中演示仿真动画采用 NAM,它 将网络仿真过程中拓扑结构、数据包传输情况及队列丢包等以动画形式展现, 可以清晰的通过过程的比较得到,在有 RTS/CTS 控制帧的网络环境中,如果存 在隐藏节点问题,可以较好的进行解决和优化,减轻网络负担和压力,优化网 络环境。而没有使用 RTS/CTS 控制帧的网络环境中,因为隐藏节点的存在,不 断的产生数据冲突,不仅加重了网络的负担,还使得很多冲突数据不可用,是 一个很严重的问题。隐藏终端的问题,通过 RTS/CTS 控制帧解决,通过通知传 送范围内的其他节点不要有动作,来避免发送冲突。 2.对设计及调试过程的心得体会。 遇到问题慢慢调试,这次学会了隐藏节点的仿真,收获颇多。尤其是在遇到问 题的情况下,一步一步debug出问题所在,这个过程是我最喜欢的,也是对我学
科成长帮助最大的环节。