您好,欢迎访问三七文档
NS-2仿真模拟实例汇总(以方路平的书为主)1.附录1简单有线网络实例(p105)#Createasimulatorobjectsetns[newSimulator]#Definedifferentcolorsfordataflows$nscolor1Blue$nscolor2Red#Openthenamtracefilesetnf[openout.namw]$nsnamtrace-all$nf#Definea'finish'procedureprocfinish{}{globalnsnf$nsflush-trace#Closethetracefileclose$nf#Executenamonthetracefileexecnamout.nam&exit0}#Createfournodessetn0[$nsnode]setn1[$nsnode]setn2[$nsnode]setn3[$nsnode]#Createlinksbetweenthenodes$nsduplex-link$n0$n21Mb10msDropTail$nsduplex-link$n1$n21Mb10msDropTail$nsduplex-link$n3$n21Mb10msSFQ$nsduplex-link-op$n0$n2orientright-down$nsduplex-link-op$n1$n2orientright-up$nsduplex-link-op$n2$n3orientright#Monitorthequeueforthelinkbetweennode2andnode3$nsduplex-link-op$n2$n3queuePos0.5#CreateaUDPagentandattachittonoden0setudp0[newAgent/UDP]$udp0setclass_1$nsattach-agent$n0$udp0#CreateaCBRtrafficsourceandattachittoudp0setcbr0[newApplication/Traffic/CBR]$cbr0setpacketSize_500$cbr0setinterval_0.005$cbr0attach-agent$udp0#CreateaUDPagentandattachittonoden1setudp1[newAgent/UDP]$udp1setclass_2$nsattach-agent$n1$udp1#CreateaCBRtrafficsourceandattachittoudp1setcbr1[newApplication/Traffic/CBR]$cbr1setpacketSize_500$cbr1setinterval_0.005$cbr1attach-agent$udp1#CreateaNullagent(atrafficsink)andattachittonoden3setnull0[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.0finish#Runthesimulation$nsrun2.附录2简单无线网络实例(p108)#Defineoptions#=========================================================setval(chan)Channel/WirelessChannelsetval(prop)Propagation/TwoRayGroundsetval(netif)Phy/WirelessPhysetval(mac)Mac/802_11setval(ifq)Queue/DropTail/PriQueuesetval(ll)LLsetval(ant)Antenna/OmniAntennasetval(x)1000;#Xdimensionofthetopographysetval(y)1000;#Ydimensionofthetopographysetval(cp)setval(sc)setval(ifqlen)50;#maxpacketinifqsetval(nn)3;#howmanynodesaresimulatedsetval(seed)0.0setval(stop)100.0;#simulationtimesetval(tr)exp.trsetval(rp)DSDVsetAgentTraceONsetRouterTraceONsetMacTraceOFF#========================================================#MainProgram#========================================================setns[newSimulator]$nscolor1blue$nscolor2red#$nsuse-newtrace#setnamfd[opennam-exp.trw]#$nsnamtrace-all-wireless$namfd$val(x)$val(y)#settracefd[openexp.trw]settracefd[openexp.trw]$nstrace-all$tracefdsetnamfd[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\-movementTraceOFFfor{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-dist011setgod[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.0setudp(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)$nsat10.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)#$nsat10.0$cbr(1)startfor{seti0}{$i$val(nn)}{incri}{$nsinitial_node_pos$node($i)30}for{seti0}{$i$val(nn)}{incri}{$nsat$val(stop)$node($i)reset;}#$nsat$val(stop)puts\NSEXITING\;$nshalt#$nsat$val(stop)stopputs$tracefdM0.0nn$val(nn)x$val(x)y$val(y)rp$val(rp)puts$tracefdM0.0sc$val(sc)cp$val(cp)seed$val(seed)puts$tracefdM0.0prop$val(prop)ant$val(ant)putsStartingSimulation...#procstop{}{#globalnstracefdnamfd#$nsflush-trace#close$tracefd#close$namfd#execnamexp.namexit0#}procfinish{}{globalnstracefdnamfd$nsflush-traceclose$tracefdclose$namfdexecnamexp.nam&exit0}$nsat$val(stop)finish$nsrun3.TCP滑动窗口及慢启动算法模拟(P119)if{$argc==4}{setbandwidth[lindex$argv0]setdelay[lindex$argv1]setwindow[lindex$argv2]settime[lindex$argv3]}else{putsbandwidthputsn0----------n1putsTCP_windowdelayputsUsage:$argv0bandwidthdelaywindowsimulation_timeexit1}setns[newSimulator]setnf[openout.namw]$nsnamtrace-all$nfsetftr[openslidewin.trw]$nstrace-all$ftrprocfinish{}{globalnsnf$nsflush-traceclose$nfexecnamout.nam&exit0}setn0[$nsnode]setn1[$nsnode]$nsduplex-link$n0$n1$bandwidth$delayDropTail$nsduplex-l
本文标题:NS2仿真实例汇总
链接地址:https://www.777doc.com/doc-6038048 .html