收藏 分销(赏)

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

上传人:a199****6536 文档编号:1830454 上传时间:2024-05-09 格式:DOC 页数:113 大小:383KB 下载积分:20 金币
下载 相关 举报
NS网络模拟和协议仿真源代码.doc_第1页
第1页 / 共113页
NS网络模拟和协议仿真源代码.doc_第2页
第2页 / 共113页


点击查看更多>>
资源描述
第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 {} { global ns tracefd nf $ns flush-trace close $tracefd close $nf exit 0 } #定义节点 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 $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 "$cbr start" $ns at 1.0 "$ftp start" $ns at 9.0 "$ftp stop" $ns at 9.5 "$cbr stop" $ns at 9.5 "$ns detach-agent $n0 $tcp; $ns detach-agent $n5 $sink" #仿真结束时调用结束进程 $ns at 10.0 "finish" #打印CBR数据包的大小和间隔 puts "CBR packet_size_ = [$cbr set packet_size_]" puts "CBR interval = [$cbr set interval_]" #执行模拟 $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 ;# time of simulation end # 建立一个simulator实例 set ns [new Simulator] #$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 "$n($i) reset"; } #启动和结束流代理 $ns at 0.5 "$cbr start" $ns at 9.5 "$cbr stop" #定义结束进程 proc finish {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exit 0 } #仿真结束时调用结束进程 $ns at $val(stop) "finish" $ns at $val(stop) "puts \"NS EXISTING...\"; $ns halt" puts "Start Simulation..." # 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 ;# time of simulation end # 建立一个simulator实例 set ns [new Simulator] #设定分层路由地址 $ns node-config -addressType hierarchical AddrParams set domain_num_ 2 ;# 2个网络 lappend cluster_num 1 1 ;# 每个网络一个子网 AddrParams set cluster_num_ $cluster_num lappend eilastlevel 1 3 ;# 2个子网的节点数目为1和3 AddrParams set nodes_num_ $eilastlevel puts "Configuration of hierarchical addressing done" #$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 "$ugs0 start" $ns at 1.0 "$ugs1 start" $ns at 9.0 "$ugs1 stop" $ns at 9.5 "$ugs0 stop" proc finish {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exit 0 } #事件安排 $ns at $val(stop) "finish" $ns at $val(stop) "puts \"NS EXISTING...\"; $ns halt" puts "Start Simulation..." # run the simulation $ns run 第10章 delay.awk BEGIN { highest_uid=0; } { event = $1; time = $2; node = $3; len = length(node); if(len == 3) { node_ = substr(node,2,1); trace_type = $4; flag = $5; uid = $6; pkt_type = $7; pkt_size = $8; } else { from_node = $3; to_node =$4; pkt_type = $5; pkt_size = $6; flag = $7; uid = $12; } if(len==3) { if(event=="s" && node_==2 && trace_type=="AGT" && pkt_type=="cbr") start_time[uid] = time; } else { if(event=="r" && to_node==0 && pkt_type=="cbr") end_time[uid] = time; } if(highest_uid < uid) highest_uid = uid; } END { id=1; k=0; total_delay=0; avg_delay=0; for(i=0; i<=highest_uid; i++ ) { start = start_time[i]; end = end_time[i]; delay = end - start; if(delay > 0) { total_delay=total_delay+delay; k++; } } avg_delay=total_delay/k; printf("%f %.9f\n",rate,avg_delay); } get_perform #!/bin/sh i=1 while [ "$i" -le 300 ]; do ns w_w.tcl $i gawk –f delay.awk rate=$i wired_wireless.tr >>delay gawk –f loss.awk rate=$i wired_wireless.tr >>loss_rate gawk -f throughtput.awk rate=$i wired_wireless.tr >>throughtput if ["$i" -le 225 ]; then i=$(($i+20)) elif ["$i" -le 250 ]; then i=$(($i+5)) else i=$(($i+2)) fi done exit 0 jitter.awk BEGIN { highest_uid=0; } { event = $1; time = $2; node = $3; len = length(node); if(len == 3) { node_ = substr(node,2,1); trace_type = $4; flag = $5; uid = $7; pkt_type = $8; pkt_size = $9; } else { from_node = $3; to_node =$4; pkt_type = $5; pkt_size = $6; flag = $7; uid = $12; } if(len==3) { if(event=="s" && node_==2 && trace_type=="AGT" && pkt_type=="cbr") start_time[uid] = time; } else { if(event=="r" && to_node==0 && pkt_type=="cbr") end_time[uid] = time; } if(highest_uid < uid) highest_uid = uid; } END { id=1; for(i=0; i<=highest_uid; i++ ) { start = start_time[i]; end = end_time[i]; if(start < end) { delay[i]=end-start; } } last_delay=0; last_seqno=0; for(i=0; i<=highest_uid; i++) { if(delay[i]!=0) { if(last_delay==0) { jitter=0; } else { jitter=(delay[i]-last_delay)/(i-last_seqno); } printf("%d %.8f\n",i,jitter); last_delay=delay[i]; last_seqno=i; } } } loss.awk BEGIN { fsDrops=0; numfs2=0; numfs0=0; } { event = $1; time = $2; node = $3; len = length(node); if(len == 3) { node_ = substr(node,2,1); trace_type = $4; flag = $5; uid = $6; pkt_type = $7; pkt_size = $8; } else { from_node = $3; to_node =$4; pkt_type = $5; pkt_size = $6; flag = $7; uid = $12; } if(len==3) { if(event=="s" && node_==2 && trace_type=="AGT" && pkt_type=="cbr") numfs2++; } else { if(event=="r" && to_node==0 && pkt_type=="cbr") numfs0++; } } END { loss_rate=0; fsDrops=numfs2-numfs0; loss_rate=fsDrops/numfs2; printf("%f %.3f\n",rate,loss_rate); } plot_data set multiplot set size 0.5,0.5 set origin 0.0,0.0 set xrange [225:250] set xlabel "data_rate(Kbps)" set ylabel "pkt_delay(s)" plot "delay" with linespoints lw 2 pt 19 set origin 0.5,0.0 set ylabel "loss_rate(100%)" plot "loss" with linespoints lw 2 pt 19 set origin 0.0,0.5 set ylabel "throughtput(Kbps)" plot "throughtput" with linespoints lw 2 pt 19 throughtput.awk BEGIN { init=0; i=0; } { event = $1; time = $2; node = $3; len = length(node); if(len == 3) { node_ = substr(node,2,1); trace_type = $4; flag = $5; uid = $6; pkt_type = $7; pkt_size = $8; } else { from_node = $3; to_node =$4; pkt_type = $5; pkt_size = $6; flag = $7; uid = $12; } if(len==1) { if(event=="r" && to_node==0 && pkt_type=="cbr") { pkt_byte_sum[i+1]=pkt_byte_sum[i]+(pkt_size-20); if(init==0) { start_time=time; init=1; } end_time[i]=time; i++; } } } END { th = 8*pkt_byte_sum[i-1]/(end_time[i-1]-start_time)/1000; printf("%f %.3f\n", rate, th); } wire_wireless.tcl ############################################################################### ##############################有线+无线网络混合模拟############################# ############################################################################### #增加全局变量opt(rate)和过程getopt set opt(rate) 0 ;#变量opt(rate)保存数据速率值 proc getopt {argc argv} { ;#过程getopt获取从命令行传递过来的数据速率值 global opt set opt(rate) [lindex $argv 0] } #无线节点参数 set val(chan) Channel/WirelessChannel set val(prop) Propagation/TwoRayGround set val(netif) Phy/WirelessPhy set val(mac) Mac/802_11 set val(ifq) Queue/DropTail/PriQueue set val(ll) LL set val(ant) Antenna/OmniAntenna set val(ifqlen) 50 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 ;# 2个网络 lappend cluster_num 1 1 ;# 每个网络一个子网 AddrParams set cluster_num_ $cluster_num lappend eilastlevel 1 3 ;# 2个子网的节点数目为1和3 AddrParams set nodes_num_ $eilastlevel puts "Configuration of hierarchical addressing done" #$ns use-newtrace #设置trace set tracefd [open wired_wireless.tr w] set namtrace [open wired_wireless.nam w] $ns trace-all $tracefd $ns namtrace-all-wireless $namtrace $val(x) $val(y)
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服