日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ns2相关学习——TCL脚本编写(2)

發布時間:2024/8/23 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ns2相关学习——TCL脚本编写(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面來學習更加復雜一點的TCL腳本的編寫

簡述:建立有4個節點的拓撲,其中一個節點作為路由器,用來將兩個節點發出的數據包轉發到第四個節點上面。

在這里將解釋將兩個節點的數據流區分開來的方法,展示如何去檢測一個隊列是否是滿的,以及一個數據包是如何被丟棄的。

1、建立拓撲

Tcl腳本大多是大同的,我們需要做的是在基礎上進行自己需要的修改。基礎代碼如下:

#Create a simulator object set ns [new Simulator]#Open the nam trace file set nf [open out.nam w] $ns namtrace-all $nf#Define a 'finish' procedure proc finish {} {global ns nf$ns flush-trace #Close the trace fileclose $nf #Execute nam on the trace fileexec nam out.nam &exit 0 } # Insert your own code for topology creation # and agent definitions, etc. here#Call the finish procedure after 5 seconds simulation time $ns at 5.0 "finish"#Run the simulation $ns run


做出修改如下:

① 添加四個節點:

set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]

② 添加鏈接

$ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n3 $n2 1Mb 10ms DropTail

?其實上面這樣就可以了,但是,為了讓它更加美觀,在跑nam的時候不要太懵逼,我們需要變一下鏈接的位置,使用的語句如下:

$ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right


在改變的時候我們可以使用right, left, up, down,以及這些單詞的結合(結合方式:某-某)


2、添加事件

① 設置代理——建立起兩個CBR代理和一個NULL代理。CBR代理給n0和n1,n0、n1用于發送數據,n3用于接收數據。

#Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0#Create a UDP agent and attach it to node n1 set udp1 [new Agent/UDP] $ns attach-agent $n1 $udp1# Create a CBR traffic source and attach it to udp1 set cbr1 [new Application/Traffic/CBR] $cbr1 set packetSize_ 500 $cbr1 set interval_ 0.005 $cbr1 attach-agent $udp1set null0 [new Agent/Null] $ns attach-agent $n3 $null0

② CBR代理和NULL代理進行連接

$ns connect $udp0 $null0 $ns connect $udp1 $null0

③ 設置數據流開始的時間和結束的時間

$ns at 0.5 "$cbr0 start" $ns at 1.0 "$cbr1 start" $ns at 4.0 "$cbr1 stop" $ns at 4.5 "$cbr0 stop"


3、區別流

因為有倆發送端,為了區別數據流的情況,我們來給他們加點顏色;

$udp0 set class_ 1 $udp1 set class_ 2 $ns color 1 Blue $ns color 2 Red

然后這樣就udp0發出的數據流變藍色,另外一個變紅色。

4、查看鏈接的隊列

添加下面的語句來監視鏈接的情況

$ns duplex-link-op $n2 $n3 queuePos 0.5

為了得到相對公平的情況,我們可以更換使用隊列,

$ns duplex-link $n3 $n2 1Mb 10ms SFQ

SQF(隨機公平排隊)這樣最后得到的是相對公平的結果


本節完完整代碼如下:

#Create a simulator object set ns [new Simulator]#Define different colors for data flows $ns color 1 Blue $ns color 2 Red#Open the nam trace file set nf [open out.nam w] $ns namtrace-all $nf#Define a 'finish' procedure proc finish {} {global ns nf$ns flush-trace #Close the trace fileclose $nf #Execute nam on the trace fileexec nam out.nam &exit 0 }#Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]#Create links between the nodes $ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n3 $n2 1Mb 10ms SFQ$ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right#Monitor the queue for the link between node 2 and node 3 $ns duplex-link-op $n2 $n3 queuePos 0.5#Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $udp0 set class_ 1 $ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0#Create a UDP agent and attach it to node n1 set udp1 [new Agent/UDP] $udp1 set class_ 2 $ns attach-agent $n1 $udp1# Create a CBR traffic source and attach it to udp1 set cbr1 [new Application/Traffic/CBR] $cbr1 set packetSize_ 500 $cbr1 set interval_ 0.005 $cbr1 attach-agent $udp1#Create a Null agent (a traffic sink) and attach it to node n3 set null0 [new Agent/Null] $ns attach-agent $n3 $null0#Connect the traffic sources with the traffic sink $ns connect $udp0 $null0 $ns connect $udp1 $null0#Schedule events for the CBR agents $ns at 0.5 "$cbr0 start" $ns at 1.0 "$cbr1 start" $ns at 4.0 "$cbr1 stop" $ns at 4.5 "$cbr0 stop" #Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish"#Run the simulation $ns run

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的ns2相关学习——TCL脚本编写(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。