http://blog.sina.com.cn/s/blog_67a12ca70100t1za.html
本博文的实验取材与《NS-2网络模拟基础与应用》无线网络实验。
场景是三个节点,在一个矩形区域,移动Ad hoc网络使用DSDV路由算法,然后在适当的时候设置它们的移动方向、速度,并在节点0和节点1之间尝试建立ftp连接。脚本代码如下:
set val(chan)
set val(prop)
set val(netif)
set val(mac)
set val(ifq)
set val(ll)
set val(ant)
set val(ifqlen)
set val(nn)
set val(rp)
set val(x)
set val(y)
set val(stop)
#初始化模拟参数和跟踪对象
set ns [new Simulator]
set tracefd [open simple.tr w]
set windowVsTime2 [open win.tr w]
set namtrace [open simwrls.nam w]
$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
#创建移动拓扑
set topo [new Topography]
#设定移动场景范围
$topo load_flatgrid $val(x) $val(y)
#创建God对象
create-god $val(nn)
#创建$val(nn)个移动节点并将它们连接到信道。
#配置节点属性
$ns node-config -adhocRouting $val(rp) \
#创建移动节点
for {set i 0} {$i < $val(nn)} {incr i} {
}
#设定移动节点的初始位置
$node_(0) set X_ 5.0
$node_(0) set Y_ 5.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 490.0
$node_(1) set Y_ 285.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 150.0
$node_(2) set Y_ 240.0
$node_(2) set Z_ 0.0
#设定移动模式
#第10s节点0以3.0m/s速度向(250,250,0)移动,其余类推
$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
#在节点node_(0)和node(1)之间创建TCP连接
set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(1) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"
#定义统计窗口大小的过程
proc plotWindow {tcpSource file} {
}
#设置在nam中移动节点显示的大小,否则,nam中无法显示节点
for {set i 0} {$i < $val(nn)} {incr i} {
}
#模拟结束后重设节点
for {set i 0} {$i < $val(nn)} {incr i} {
}
#调度整个模拟过程的运行
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
#$ns at 150.01 "puts \" end simulation\" ;$ns halt"
proc stop {} {
}
$ns run
}
与有线网络相比,无线网络的脚本多了许多配置,但基本的配置也是固定的类型,做过几个实验后,就会慢慢适应有线网络的配置。
本文介绍了一个使用NS-2模拟器搭建的移动Adhoc网络实验,该实验包含三个移动节点,采用DSDV路由协议,并在节点间建立了FTP连接。通过详细脚本展示了网络配置、节点移动设置及流量传输过程。
1221

被折叠的 条评论
为什么被折叠?



