NS网络模拟和协议仿真源代码 下载本文

内容发布更新时间 : 2024/6/1 12:40:58星期一 下面是文章的全部内容请认真阅读。

第4章

例1

#建立一个模拟

set ns [new Simulator]

#定义不同数据流的颜色(NAM显示时用到) $ns color 1 Blue $ns color 2 Red

#开启Trace跟踪和NAM跟踪 set tracefd [open wired.tr w] $ns trace-all $tracefd set nf [open wired.nam w] $ns namtrace-all $nf

#定义结束进程 proc finish {} { }

#定义节点

set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] #$n5 color black $n5 color red

#定义节点间的链路

$ns duplex-link $n0 $n1 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n4 2Mb 20ms DropTail

global ns tracefd nf $ns flush-trace close $tracefd close $nf exit 0

$ns duplex-link $n3 $n4 2Mb 10ms DropTail $ns duplex-link $n4 $n5 2Mb 10ms DropTail

#定义链路的队列长度 $ns queue-limit $n1 $n4 10

#指定节点间的相互位置(NAM显示用到)

$ns duplex-link-op $n0 $n1 orient right-down $ns duplex-link-op $n2 $n1 orient right-up $ns duplex-link-op $n1 $n4 orient right $ns duplex-link-op $n3 $n4 orient left-down $ns duplex-link-op $n5 $n4 orient left-up

#监视链路的队列

$ns duplex-link-op $n1 $n4 queuePos 0.5

#建立一个TCP连接

set tcp [new Agent/TCP] $tcp set class_ 2

$ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n5 $sink $ns connect $tcp $sink $tcp set fid_ 1

#在TCP连接上建立FTP流

set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP

#建立一个UDP代理

set udp [new Agent/UDP] ;#建立一个数据发送代理

$ns attach-agent $n2 $udp ;#将数据发送代理绑定到发送节点 set null [new Agent/Null] ;#建立一个数据接收代理

$ns attach-agent $n3 $null ;#将数据接收代理绑定到接收节点

$ns connect $udp $null ;#连接两个代理(也就决定了数据包的发送和接收节点) $udp set fid_ 2

#在UDP代理上建立CBR流

set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR

$cbr set packet_size_ 1000 $cbr set rate_ 1mb

$cbr set random_ false

#启动和结束流代理 $ns at 0.5 \$ns at 1.0 \$ns at 9.0 \$ns at 9.5 \

$ns at 9.5 \

#仿真结束时调用结束进程 $ns at 10.0 \

#打印CBR数据包的大小和间隔

puts \puts \

#执行模拟 $ns run

例2

#简单无线网络模型模拟

#无线节点参数

set val(chan) Channel/WirelessChannel ;# channel type 信道类型:无线信道 set val(prop) Propagation/TwoRayGround ;# radio-propagation model 信道模型:TwoRayGround

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 set val(rp) AODV ;# 路由协议 set val(x) 600 ;# 拓扑-长度 set val(y) 200 ;# 拓扑-宽度 set val(stop) 10.0

# 建立一个simulator实例 set ns [new Simulator]

;# time of simulation end

#$ns use-newtrace

#开启Trace跟踪和NAM跟踪

set tracefd [open wireless.tr w] set namtrace [open wireless.nam w] $ns trace-all $tracefd

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

#建立topology对象

set topo [new Topography]

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

#创建god create-god 3

set chan_1_ [new $val(chan)]

#配置无线节点(包括使用何种路由协议,何种mac协议,无线信道的模型等等) $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_1_ \\

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

#建立无线节点并设置节点的位置(节点位置决定了拓扑结构) set n(0) [$ns node] #$n(0) shape hexagon #$n(0) label n0 #$n(0) label-color Red $n(0) random-motion 0 $n(0) set X_ 100.0 $n(0) set Y_ 100.0 $n(0) set Z_ 0.0

$ns initial_node_pos $n(0) 20

set n(1) [$ns node] $n(1) random-motion 0 $n(1) set X_ 300.0 $n(1) set Y_ 100.0 $n(1) set Z_ 0.0

$ns initial_node_pos $n(1) 20

set n(2) [$ns node] $n(2) random-motion 0 $n(2) set X_ 500.0 $n(2) set Y_ 100.0 $n(2) set Z_ 0.0

$ns initial_node_pos $n(2) 20

#建立一个UDP代理

set udp0 [new Agent/UDP] ;#$ns attach-agent $n(0) $udp0 ;#set null0 [new Agent/Null] ;#$ns attach-agent $n(2) $null0 ;#$ns connect $udp0 $null0 ;#收节点)

#在UDP代理上建立CBR流

set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp0

# 仿真结束时重置节点

for {set i 0} {$i < 3 } {incr i} { $ns at 10.0 \

}

#启动和结束流代理 $ns at 0.5 \$ns at 9.5 \

#定义结束进程 proc finish {} {

global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace

建立一个数据发送代理

将数据发送代理绑定到发送节点 建立一个数据接收代理

将数据接收代理绑定到接收节点

连接两个代理(也就决定了数据包的发送和接