NS-2仿真模拟实例汇总(以方路平的书为主)1.例4.5a:DropTail队列管理的NS_2模拟实例(P161)setns[newSimulator]setnf[openout.namw]$nsnamtrace-all$nfsettf[openout.trw]setwindowVsTime[openwinw]setparam[openparametersw]$nstrace-all$tfprocfinish{}{globalnsnftf$nsflush-traceclose$nfclose$tfexecnamout.nam&exit0}setn2[$nsnode]setn3[$nsnode]$nsduplex-link$n2$n30.7Mb20msDropTailsetNumbSrc3setDuration50for{setj1}{$j=$NumbSrc}{incrj}{setS($j)[$nsnode]}setrng[newRNG]$rngseed2setRVstart[newRandomVariable/Uniform]$RVstartsetmin_0$RVstartsetmax_7$RVstartuse-rng$rngfor{seti1}{$i=$NumbSrc}{incri}{setstartT($i)[expr[$RVstartvalue]]setdly($i)1puts$paramstartT($i)$startT($i)sec}for{setj1}{$j=$NumbSrc}{incrj}{$nsduplex-link$S($j)$n210Mb$dly($j)msDropTail$nsqueue-limit$S($j)$n220}$nsqueue-limit$n2$n3100for{setj1}{$j=$NumbSrc}{incrj}{settcp_src($j)[newAgent/TCP/Reno]$tcp_src($j)setwindow_8000}for{setj1}{$j=$NumbSrc}{incrj}{settcp_snk($j)[newAgent/TCPSink]}for{setj1}{$j=$NumbSrc}{incrj}{$nsattach-agent$S($j)$tcp_src($j)$nsattach-agent$n3$tcp_snk($j)$nsconnect$tcp_src($j)$tcp_snk($j)}for{setj1}{$j=$NumbSrc}{incrj}{setftp($j)[$tcp_src($j)attach-sourceFTP]}for{setj1}{$j=$NumbSrc}{incrj}{$tcp_src($j)setpacketSize_552}for{seti1}{$i=$NumbSrc}{incri}{$nsat$startT($i)$ftp($i)start$nsat$Duration$ftp($i)stop}procplotWindow{tcpSourcefilek}{globalnsNumbSrcsettime0.03setnow[$nsnow]setcwnd[$tcpSourcesetcwnd_]if{$k==1}{puts-nonewline$file$now\t$cwnd\t}else{if{$k$NumbSrc}{puts-nonewline$file$cwnd\t}}if{$k==$NumbSrc}{puts-nonewline$file$cwnd\n}$nsat[expr$now+$time]plotWindow$tcpSource$file$k}for{setj1}{$j=$NumbSrc}{incrj}{$nsat0.1plotWindow$tcp_src($j)$windowVsTime$j}$nsmonitor-queue$n2$n3[openqueue.trw]0.05[$nslink$n2$n3]queue-sample-timeout$nsat[expr$Duration]finish$nsrun2.例4.5b:RED队列管理的NS-2模拟实例(P164)setns[newSimulator]setnf[openout.namw]$nsnamtrace-all$nfsettf[openout.trw]setwindowVsTime[openwinw]setparam[openparametersw]$nstrace-all$tfprocfinish{}{globalnsnftf$nsflush-traceclose$nfclose$tfexecnamout.nam&exit0}setn2[$nsnode]setn3[$nsnode]$nsduplex-link$n2$n30.7Mb20msREDsetNumbSrc3setDuration50for{setj1}{$j=$NumbSrc}{incrj}{setS($j)[$nsnode]}setrng[newRNG]$rngseed2setRVstart[newRandomVariable/Uniform]$RVstartsetmin_0$RVstartsetmax_7$RVstartuse-rng$rngfor{seti1}{$i=$NumbSrc}{incri}{setstartT($i)[expr[$RVstartvalue]]setdly($i)1puts$paramstartT($i)$startT($i)sec}for{setj1}{$j=$NumbSrc}{incrj}{$nsduplex-link$S($j)$n210Mb$dly($j)msDropTail$nsqueue-limit$S($j)$n220}$nsqueue-limit$n2$n3100for{setj1}{$j=$NumbSrc}{incrj}{settcp_src($j)[newAgent/TCP/Reno]$tcp_src($j)setwindow_8000}for{setj1}{$j=$NumbSrc}{incrj}{settcp_snk($j)[newAgent/TCPSink]}for{setj1}{$j=$NumbSrc}{incrj}{$nsattach-agent$S($j)$tcp_src($j)$nsattach-agent$n3$tcp_snk($j)$nsconnect$tcp_src($j)$tcp_snk($j)}for{setj1}{$j=$NumbSrc}{incrj}{setftp($j)[$tcp_src($j)attach-sourceFTP]}for{setj1}{$j=$NumbSrc}{incrj}{$tcp_src($j)setpacketSize_552}for{seti1}{$i=$NumbSrc}{incri}{$nsat$startT($i)$ftp($i)start$nsat$Duration$ftp($i)stop}procplotWindow{tcpSourcefilek}{globalnsNumbSrcsettime0.03setnow[$nsnow]setcwnd[$tcpSourcesetcwnd_]if{$k==1}{puts-nonewline$file$now\t$cwnd\t}else{if{$k$NumbSrc}{puts-nonewline$file$cwnd\t}}if{$k==$NumbSrc}{puts-nonewline$file$cwnd\n}$nsat[expr$now+$time]plotWindow$tcpSource$file$k}for{setj1}{$j=$NumbSrc}{incrj}{$nsat0.1plotWindow$tcp_src($j)$windowVsTime$j}setredq[[$nslink$n2$n3]queue]settraceq[openred-queue.trw]$redqtracecurq_$redqtraceave_$redqattach$traceq$nsat[expr$Duration]finish$nsrun$grepared-queue.trave.tr$grepQred-queue.trcur.trgnplotsettitlecurrentandaveragequeuesizegnplotsetxlabeltimegnplotsetylabelqueuesizegnplotplot