ns无线网络仿真节点设置及说明

更新时间:2023-12-10 02:44:01 阅读量: 教育文库 文档下载

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

ns无线网络仿真节点设置及说明

要做一个4个节点的Ad hoc网络仿真,实验环境和要求是:

采 用的无线网拓扑结构,主机节点数共有4个;节点1到4的初始位置坐标分别为(0,1 000)、(0,800)、(0,600)和(400,600),节点1和2在仿真中 保持静止不动,节点3和4则以60m/s速度分别向坐标(0,0)和 (400,0)位置方向垂直移动。系统带宽为2Mbps,系统节点缺省无线传输半径约为500m 。仿真使用的业务流量为FTP,每包发送512字节,发送 速度10包/s。采用静态路由方式,节点1在1.5s发包给节点2,节点3在3.5s发包给节点4。 仿真时间一共10s,选取节点2处的估计可用带宽进行 分析。 下面是基本环境仿真的adhoc.tcl源码: # This script is created by emile.Y.S Xiang

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

# 定义模拟变量 #===================================

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

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(x) 1000 ;# X dimension of topology

set

val(y) 1000 ;# Y dimension of topology set

val(cp) \ ;# node movement model file set

val(sc) \ ;# traffic model file set val(ifqlen)

50 max packet in ifq set

val(nn) 4 ;# number of mobilenodes set val(seed) 0.0 set

val(stop) 10.0 ;# time of simulation end set

val(tr) adhoc.tr ;# trace file name set

val(rp) DSDV ;# routing protocol set AgentTrace ON set RouterTrace ON set MacTrace OFF

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

# 建立相关档案 #=================================== # Initialize Global Variables set ns_ [new

Simulator] ns simulator实例 $ns_ color 1 Blue $ns_ color 2 Red

# 设定trace file $ns_

use-newtrace ;#使用新的trace format

;# 产生 ;# set namfd [open adhoc.nam

w] ;#产生nam trace file $ns_ namtrace-all-wireless $namfd $val(x) $val(y) ;#模拟时产生需要的结果文件

set tracefd [open $val(tr)

w] ;#产生trace file $ns_ trace-all

$tracefd ;#模拟时产生需要的结果文件

#建立topology对象,以记录mobilenodes在拓扑內移动的情况 set topo [new Topography] # 拓扑的范围为 1000m x 1000m

$topo load_flatgrid $val(x) $val(y) # 建立channel

set chan [new $val(chan)] # 创建god

set god_ [create-god $val(nn)]

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

# 无线节点配置 #=================================== # Create the specified number of mobile nodes [$val(nn)] and \them to

# the channel. Four nodes are created : node(0), node(1), node(2) and node(3)

# 设定Mobile Node的参数

$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) \\ -channel $chan \\

-topoInstance $topo \\ -agentTrace ON \\ -routerTrace OFF \\ -macTrace OFF \\ -movementTrace OFF

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

# 新建Node #===================================

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

$node_($i) random-motion 0 ;# 使各节点非随机移动 }

# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes # 建立第0个Node,开始时,位置在(0.0, 1000.0) $node_(0) set X_ 0.0 $node_(0) set Y_ 1000.0 $node_(0) set Z_ 0.0

# 建立第1个Node,开始时,位置在(0.0, 800.0) $node_(1) set X_ 0.0 $node_(1) set Y_ 800.0 $node_(1) set Z_ 0.0

# 建立第2个Node,开始时,位置在(0.0, 600.0) $node_(2) set X_ 0.0 $node_(2) set Y_ 600.0 $node_(2) set Z_ 0.0

# 建立第3个Node,开始时,位置在(400.0, 600.0) $node_(3) set X_ 400.0 $node_(3) set Y_ 600.0 $node_(3) set Z_ 0.0

# Load the god object with shortest hop information # 在节点0和节点1之间最短的hop数为1 $god_ set-dist 0 1 1

# 在节点1和节点2之间最短的hop数为1 $god_ set-dist 1 2 1

# 在节点0和节点2之间最短的hop数为2 $god_ set-dist 0 2 2

# 在节点2和节点3之间最短的hop数为1 $god_ set-dist 2 3 1

# 在节点0和节点3之间最短的hop数为1 $god_ set-dist 0 3 1

# 在节点1和节点3之间最短的hop数为1 $god_ set-dist 1 3 1

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

# 产生Movement #===================================

# Now produce some simple node movements

# Node_(2) and Node_(3) starts to move downward set god_ [God instance]

#node移动不能到边界,否则会报错

# 从0秒开始,节点2开始从位置(0,600)移動到(0,0),速度為60.0 m/s $ns_ at 0.0 \

# 从0秒开始,节点3开始从位置(400,600)移動到(400,0),速度為60.0 m/s $ns_ at 0.0 \

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

# 建立FTP业务,基于TCP来承载 #===================================

# 在节点0和节点1间设定第0个连线(FTP-TCP),且在时间为1.5秒开始发送 set tcp0 [new Agent/TCP/Newreno] $tcp0 set fid_ 1

set sink0 [new Agent/TCPSink] $ns_ attach-agent $node_(0) $tcp0 $ns_ attach-agent $node_(1) $sink0 $ns_ connect $tcp0 $sink0

set ftp0 [new Application/FTP] $ftp0 attach-agent $tcp0 $ns_ at 1.5 \$ns_ at 10.0 \

# 在节点2和节点3间设定第1个连线(FTP-TCP),且在时间为3.5秒开始发送 set tcp1 [new Agent/TCP/Newreno] $tcp1 set fid_ 2

set sink1 [new Agent/TCPSink] $ns_ attach-agent $node_(2) $tcp1 $ns_ attach-agent $node_(3) $sink1 $ns_ connect $tcp1 $sink1

set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1 $ns_ at 3.5 \$ns_ at 10.0 \

# 在nam中定义节点初始所在位置

for {set i 0} {$i < $val(nn)} {incr i} {

# The function must be called after mobility model is defined. $ns_ initial_node_pos $node_($i) 60 }

# 告诉MobileNode模拟已结束

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

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

# 结束模拟 #=================================== # 结束nam与模拟器

$ns_ at $val(stop) \$ns_ at $val(stop) \

$ns_ at $val(stop) \# 设定模拟器用的stop function proc stop {} {

global ns_ tracefd namfd $ns_ flush-trace close $tracefd close $namfd

exec nam adhoc.nam & exit 0 }

puts $tracefd \puts $tracefd \puts $tracefd \puts \$ns_ run

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

Top