NS2仿真实例汇总Word文件下载.docx
《NS2仿真实例汇总Word文件下载.docx》由会员分享,可在线阅读,更多相关《NS2仿真实例汇总Word文件下载.docx(43页珍藏版)》请在冰豆网上搜索。
$nsduplex-link-op$n2$n3queuePos0.5
#CreateaUDPagentandattachittonoden0
setudp0[newAgent/UDP]
$udp0setclass_1
$nsattach-agent$n0$udp0
#CreateaCBRtrafficsourceandattachittoudp0
setcbr0[newApplication/Traffic/CBR]
$cbr0setpacketSize_500
$cbr0setinterval_0.005
$cbr0attach-agent$udp0
#CreateaUDPagentandattachittonoden1
setudp1[newAgent/UDP]
$udp1setclass_2
$nsattach-agent$n1$udp1
#CreateaCBRtrafficsourceandattachittoudp1
setcbr1[newApplication/Traffic/CBR]
$cbr1setpacketSize_500
$cbr1setinterval_0.005
$cbr1attach-agent$udp1
#CreateaNullagent(atrafficsink)andattachittonoden3
setnull0[newAgent/Null]
$nsattach-agent$n3$null0
#Connectthetrafficsourceswiththetrafficsink
$nsconnect$udp0$null0
$nsconnect$udp1$null0
#ScheduleeventsfortheCBRagents
$nsat0.5"
$cbr0start"
$nsat1.0"
$cbr1start"
$nsat4.0"
$cbr1stop"
$nsat4.5"
$cbr0stop"
#Callthefinishprocedureafter5secondsofsimulationtime
$nsat5.0"
finish"
#Runthesimulation
$nsrun
2.附录2简单无线网络实例(p108)
#Defineoptions
#=========================================================
setval(chan)Channel/WirelessChannel
setval(prop)Propagation/TwoRayGround
setval(netif)Phy/WirelessPhy
setval(mac)Mac/802_11
setval(ifq)Queue/DropTail/PriQueue
setval(ll)LL
setval(ant)Antenna/OmniAntenna
setval(x)1000;
#Xdimensionofthetopography
setval(y)1000;
#Ydimensionofthetopography
setval(cp)"
"
setval(sc)"
setval(ifqlen)50;
#maxpacketinifq
setval(nn)3;
#howmanynodesaresimulated
setval(seed)0.0
setval(stop)100.0;
#simulationtime
setval(tr)exp.tr
setval(rp)DSDV
setAgentTraceON
setRouterTraceON
setMacTraceOFF
#========================================================
#MainProgram
$nscolor1blue
$nscolor2red
#$nsuse-newtrace
#setnamfd[opennam-exp.trw]
#$nsnamtrace-all-wireless$namfd$val(x)$val(y)
#settracefd[openexp.trw]
settracefd[openexp.trw]
$nstrace-all$tracefd
setnamfd[openexp.namw]
$nsnamtrace-all-wireless$namfd$val(x)$val(y)
settopo[newTopography]
$topoload_flatgrid$val(x)$val(y)
setchan[new$val(chan)]
setgod[create-god$val(nn)]
$nsnode-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\
-agentTraceON\
-routerTraceON\
-macTraceOFF\
-movementTraceOFF
for{seti0}{$i<
$val(nn)}{incri}{
setnode($i)[$nsnode]
$node($i)random-motion0
$node(0)setX_350.0
$node(0)setY_500.0
$node(0)setZ_0.0
$node
(1)setX_500.0
$node
(1)setY_500.0
$node
(1)setZ_0.0
$node
(2)setX_650.0
$node
(2)setY_500.0
$node
(2)setZ_0.0
$godset-dist121
$godset-dist022
$godset-dist011
setgod[Godinstance]
#$nsat10.0"
$node
(1)setdest490.0100.02.0"
#x坐标不能相同
$nsat10.0"
$node
(1)setdest500.1100.02.0"
$nsat40.0"
$node
(1)setdest500.0900.02.0"
setudp(0)[newAgent/UDP]
$udp(0)setfid_1
$nsattach-agent$node(0)$udp(0)
setnull(0)[newAgent/Null]
$nsattach-agent$node
(2)$null(0)
setcbr(0)[newApplication/Traffic/CBR]
$cbr(0)setpacketSize_200
$cbr(0)setinterval_0.1
$cbr(0)setrandom_1
$cbr(0)setmaxpkts_10000
$cbr(0)attach-agent$udp(0)
$nsconnect$udp(0)$null(0)
$cbr(0)start"
#setudp
(1)[newAgent/UDP]
#$nsattach-agent$node
(2)$udp
(1)
#setnull
(1)[newAgent/Null]
#$nsattach-agent$node
(1)$null
(1)
#setcbr
(1)[newApplication/Traffic/CBR]
#$cbr
(1)setpacketSize_64
#$cbr
(1)setinterval_0.1
#$cbr(0)setrandom_1
#$cbr(0)setmaxpkts_10000
#$cbr
(1)attach-agent$udp
(1)
#$nsconnect$udp
(1)$null
(1)
$cbr
(1)start"
for{seti0}{$i<
$nsinitial_node_pos$node($i)30
$val(nn)}{incri}{
$nsat$val(stop)"
$node($i)reset"
;
#$nsat$val(stop)"
puts\"
NSEXITING\"
$nshalt"
stop"
puts$tracefd"
M0.0nn$val(nn)x$val(x)y$val(y)rp$val(rp)"
M0.0sc$val(sc)cp$val(cp)seed$val(seed)"
M0.0prop$val(prop)ant$val(ant)"
puts"
StartingSimulation..."
#procstop{}{
#globalnstracefdnamfd
#$nsflush-trace
#close$tracefd
#close$namfd
#execnamexp.nam&
#exit0
#}
procfinish{}{
globalnstracefdnamfd
$nsflush-trace
close$tracefd
close$namfd
execnamexp.nam&
exit0}
3.TCP滑动窗口及慢启动算法模拟(P119)
if{$argc==4}{
setbandwidth[lindex$argv0]
setdelay[lindex$argv1]
setwindow[lindex$argv2]
settime[lindex$argv3]
}else{
puts"
bandwidth"
n0----------n1"
TCP_windowdelay"
Usage:
$argv0bandwidthdelaywindowsimulation_time"
exit1
setftr[openslidewin.trw]
$nstrace-all$ftr
globalnsnf
close$nf
execnamout.nam&
exit0
$nsduplex-link$n0$n1$bandwidth$delayDropTail
$nsduplex-link-op$n0$n1orientleft-right
settcp[$nscreate-connectionTCP/RFC793edu$n0TCPSink$n11]
$tcpsetwindow_$window
$tcpsetssthresh_60
$tcpsetpacketSize_500
setftp[newApplication/FTP]
$ftpattach-agent$tcp
$ftpsettype_FTP
#setf0[opencwndrecNoss.trw]
#procRecord{}
#{
#globalf0tcpns
#setintval0.1
#setnow[$nsnow]
#setcwnd[$tcpsetcwnd_]
#puts$f0"
$now$cwnd"
#$nsat[expr$now+$intval]"
Record"
#$nsat0.1"
$ftpstart"
$nsat$time"
4.例4.1b:
快速重传和快速恢复模拟(P123)
setnf[openexample4.1b1.namw]
setnd[openexample4.1b1.trw]
$nstrace-all$nd
globalnsnfnd
close$nf
close$nd
execnamexample4.1b1.nam&
foreachi"
0123"
{
setn$i[$nsnode]
$nsduplex-link$n0$n15Mb20msDropTail
$nsduplex-link$n1$n20.2Mb100msDropTail
$nsduplex-link$n2$n35Mb20msDropTail
$nsqueue-limit$n1$n25
$nsduplex-link-op$n0$n1orientright-up
$nsduplex-link-op$n1$n2orientright
$nsduplex-link-op$n2$n3orientright-down
$nsduplex-link-op$n1$n2queuePos0.5
settcp[newAgent/TCP/Reno]
$nsattach-agent$n0$tcp
setsink[newAgent/TCPSink]
$nsattach-agent$n3$sink
$nsconnect$tcp$sink
$nsat0.0"
$n0labelTCP"
$n3labelTCP"
$nstrace-annotate\"
TCPReno:
FastRecovery\"
$nsat0.1"
$ftpstop"
$nsat5.25"
$tcpsetnam_tracevar_true
$nsadd-agent-trace$tcptcp
$nsmonitor-agent-trace$tcp
$tcptracevarcwnd_
$tcptracevarssthresh_
$tcptracevarmaxseq_
$tcptracevarack_
$tcptracevardupacks_
5.例4.2:
LAN模拟示例(P130)
setopt(tr)"
out.tr"
setopt(namtr)"
lantest.nam"
setopt(stop)5
setopt(node)8
setopt(qsize)100
setopt(bw)10Mb
setopt(delay)10ms
setopt(ll)LL
setopt(ifq)Queue/DropTail
setopt(mac)Mac/802_3
setopt(chan)Channel
setopt(tcp)TCP/Reno
setopt(sink)TCPSink
setopt(app)FTP
globalnsopttrfd
close$trfd
execnamlantest.nam&
exit0
}
proccreate-trace{}{
globalnsopt
if[fileexists$opt(tr)]{
catch"
execrm-f$opt(tr)$opt(tr)-bw[glob$opt(tr)*]"
settrfd[open$opt(tr)w]
$nstrace-all$trfd
if{$opt(namtr)!
="
}{
$nsnamtrace-all[open$opt(namtr)w]
return$trfd
proccreate-topology{}{
globallannodesourcenode0
setnum$opt(node)
for{seti0}{$i<
$num}{incri}{
setnode($i)[$nsnode]
lappendnodelist$node($i)
}
setlan[$nsnewLan$nodelist$opt(bw)$opt(delay)\
-llType$opt(ll)\
-macType$opt(mac)\
-ifqType$opt(ifq)\
-chanType$opt(chan)]
setnode0[$nsnode]
$nsduplex-link$node0$node(0)2Mb2msDropTail
$nsduplex-link-op$node0$node(0)orientright
settrfd[create-trace]
create-topology
settcp0[$nscreate-connectionTCP/Reno$node(7)TCPSink$node00]
$tcp0setwindow_32
setftp0[$tcp0attach-appFTP]
settcp1[$nscreate-connectionTCP/Reno$node
(2)TCPSink$node00]
$tcp1setwindow_32
setftp1[$tcp1attach-appFTP]
settcp2[$nscreate-connectionTCP/Reno$node(4)TCPSink$node00]
$tcp2setwindow_32
setftp2[$tcp2attach-appFTP]
$ftp0start"
$ftp1start"
$ftp2start"
$nsat$opt(stop)"
6.例4.3a:
单播路由模拟(P141)
setfile2[openout.namw]
$nsnamtrace-all$file2
setfile1[openout.trw]
$nstrace-a