NS网络模拟和协议仿真源代码

}

exit 0

#仿真结束时调用结束进程 $ns at $val(stop) \

$ns at $val(stop) \

puts \

# run the simulation $ns run

例3

#有线无线网络混合模拟

#无线节点参数

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) DSDV ;#路由协议 set val(nn) 4 ;#节点数目 set val(x) 600 ; set val(y) 600 ; set val(stop) 10.0

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

#设定分层路由地址

$ns node-config -addressType hierarchical AddrParams set domain_num_ 2 lappend cluster_num 1 1

AddrParams set cluster_num_ $cluster_num

;# 2个网络 ;# 每个网络一个子网

;# time of simulation end

lappend eilastlevel 1 3 ;# 2个子网的节点数目为1和3 AddrParams set nodes_num_ $eilastlevel

puts \

#$ns use-newtrace

#设置trace

set tracefd [open wired_wireless2.tr w] set namtrace [open wired_wireless2.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 $val(nn)

set sinkNode [$ns node 0.0.0] $sinkNode set X_ 500 $sinkNode set Y_ 300 $sinkNode set Z_ 0

$ns initial_node_pos $sinkNode 60

set chan_1_ [new $val(chan)]

#无线节点配置

$ns node-config -wiredRouting ON \\ -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 #新建BS节点

set bs(0) [$ns node 1.0.0] $bs(0) random-motion 0 #节点标签与初始位置设定 $bs(0) set X_ 200.0 $bs(0) set Y_ 300.0 $bs(0) set Z_ 0.0

$ns initial_node_pos $bs(0) 60

#定义节点间的链路

$ns duplex-link $sinkNode $bs(0) 10Mb 1ms DropTail

$ns duplex-link-op $sinkNode $bs(0) orient left

#定义链路的队列长度

#$ns queue-limit $bs(0) $sinkNode 10

#监视链路的队列

#$ns duplex-link-op $bs(0) $sinkNode queuePos 0.5

$ns node-config -wiredRouting OFF \\ -macType Mac/802_11

#新建SS节点

set ss(0) [$ns node 1.0.1]

$ss(0) base-station [AddrParams addr2id [$bs(0) node-addr]] $ss(0) set X_ 50.0 $ss(0) set Y_ 450.0 $ss(0) set Z_ 0.0

$ns initial_node_pos $ss(0) 60

set ss(1) [$ns node 1.0.2]

$ss(1) base-station [AddrParams addr2id [$bs(0) node-addr]] $ss(1) set X_ 50.0 $ss(1) set Y_ 150.0 $ss(1) set Z_ 0.0

$ns initial_node_pos $ss(1) 60

set udp0 [new Agent/UDP] $ns attach-agent $ss(0) $udp0

set null0 [new Agent/Null] $ns attach-agent $sinkNode $null0 $ns connect $udp0 $null0

set ugs0 [new Application/Traffic/UGS] $ugs0 attach-agent $udp0

set udp1 [new Agent/UDP]

$ns attach-agent $sinkNode $udp1 set null1 [new Agent/Null] $ns attach-agent $ss(1) $null1 $ns connect $udp1 $null1

set ugs1 [new Application/Traffic/UGS] $ugs1 attach-agent $udp1

$ns at 0.5 \$ns at 1.0 \$ns at 9.0 \$ns at 9.5 \

proc finish {} {

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

#事件安排

$ns at $val(stop) \

$ns at $val(stop) \

puts \

# run the simulation $ns run

exit 0

第10章

delay.awk

BEGIN { } { } END {

k=0;

total_delay=0; avg_delay=0; id=1; event = $1; time = $2; node = $3;

len = length(node); if(len == 3) { }

if(len==3) { }

if(highest_uid < uid)

highest_uid = uid;

if(event==\

start_time[uid] = time; node_ = substr(node,2,1); trace_type = $4; flag = $5; uid = $6; pkt_type = $7; pkt_size = $8; from_node = $3; to_node =$4; pkt_type = $5; pkt_size = $6; flag = $7; uid = $12; highest_uid=0;

} else {

} else {

if(event==\

end_time[uid] = time;

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4