天天看點

NS2入門執行個體 2---無線網絡,兩節點(TCP連接配接,FTP應用)

#場景描述:

#無線網絡中,兩個節點  node_(0) 和 node_(1), TCP+FTP, 并且設定了節點的移動

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

# 無線節點的參數設定

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

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(ifqlen)         50                         ;# max packet in ifq

set val(nn)             2                          ;# number of mobilenodes

set val(rp)             AODV                       ;# routing protocol

set val(x)              500                        ;# X dimension of the topography

set val(y)              500                        ;# Y dimension of the topography

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

# 建立Simulator對象,用于模拟過程的事件排程

set ns [new Simulator]

#設定相關記錄檔案

set tracefd [open example2.tr w]

$ns trace-all $tracefd

set namtracefd [open example2.nam w]

# 注意: 與有線場景的指令有差别哦!

$ns namtrace-all-wireless $namtracefd $val(x) $val(y)

# 設定模拟結束時的操作, 将記錄寫入檔案,并關閉檔案, 最後啟動 NAM 進行動畫顯示

proc finish {} {

global ns tracefd namtracefd

$ns flush-trace

 close $tracefd

close $namtracefd

exec nam example2.nam &

exit 0    

}

# 建立一個Topography對象,該對象保證移動節點會在拓撲邊界範圍内運動

set topo [new Topography]

# 500 X 500的邊界

$topo load_flatgrid $val(x) $val(y)

# God對象主要用來對路由協定做性能評價,

# 它存儲了: 節點的總數、各個節點間最短路徑表等資訊, 這些資訊通常在模拟開始之前就計算好了!

# 節點的MAC對象會調用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  

# 建立兩個節點,存儲在數組 Node中,    

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

set node_($i) [$ns node] 

$node_($i) random-motion 0  ;# disable random motion

}

#設定節點的實體位置,一般第三位Z_=0.0, 模拟過程實際上是在而為平面上的場景

$node_(0) set X_ 5.0

$node_(0) set Y_ 2.0

$node_(0) set Z_ 0.0

$node_(1) set X_ 390.0

$node_(1) set Y_ 385.0

$node_(1) set Z_ 0.0

#設定節點的移動, setdest 20.0 18.0 1.0:  向(20.0,18.0)位置以 1.0m/s的速度移動!

$ns at 1.0 "$node_(0) setdest 20.0 18.0 1.0"

$ns at 5.0 "$node_(1) setdest 25.0 20.0 15.0"

$ns at 100.0 "$node_(1) setdest 490.0 480.0 15.0"

# 建立TCP,及TCP對應的TCPSink,并連接配接起來,最後在TCP連接配接上添加FTP應用

set tcp [new Agent/TCP]

$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

#設定FTP資料流的開始時間

$ns at 1.0 "$ftp start" 

#模拟結束前調用各節點的reset函數, 無線場景中,一般照寫就可!

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

$ns at 150.0 "$node_($i) reset";

}

$ns at 150.0 "finish"

$ns run

NS2入門執行個體 2---無線網絡,兩節點(TCP連接配接,FTP應用)

繼續閱讀