使用TCL的NS2网络模拟器 - 基于UDP的CBR

时间:2015-04-26 16:12:48

标签: networking awk udp tcl ns2

我正在开展一个项目,我在模拟工作时遇到了问题。我总共有22个节点,当我建立连接时,我没有在某些链接上获取数据包。更具体地说,我从节点4到节点2以及节点2的任何子节点都有连接和成功的数据包发送。当我尝试在其他任何地方发送数据包时,都没有传送。我彻底查看了我的代码,似乎无法弄清楚我做错了什么。任何指向正确的方向将不胜感激。

Main File

   #Create a simulator object
   set ns [new Simulator]

   #Tell the simulator to use dynamic routing 
   $ns rtproto DV

   #Create a trace file
   set tracefd [open project2trace.tr w]
   $ns trace-all $tracefd

   #Define a ‘finish’ procedure
   proc finish {} {
    global ns nf

    $ns flush-trace
    exit 0
   }


   #Create nodes
   set n1 [$ns node]
   set n2 [$ns node]
   set n3 [$ns node]
   set n4 [$ns node]
   set n5 [$ns node]
   set n6 [$ns node]
   set n7 [$ns node]
   set n8 [$ns node]
   set n9 [$ns node]
   set n10 [$ns node]
   set n11 [$ns node]
   set n12 [$ns node]
   set n13 [$ns node]
   set n14 [$ns node]
   set n15 [$ns node]
   set n16 [$ns node]
   set n17 [$ns node]
   set n18 [$ns node]
   set n19 [$ns node]
   set n20 [$ns node]
   set n21 [$ns node]
   set n22 [$ns node]


   #Create 1Mbps connections
   #Create a duplex link between the nodes 1 and subnodes
   $ns duplex-link $n1 $n11 1Mb 10ms DropTail
   $ns duplex-link $n1 $n12 1Mb 10ms DropTail
   $ns duplex-link $n1 $n13 1Mb 10ms DropTail
   $ns duplex-link $n1 $n14 1Mb 10ms DropTail

   #Create a duplex link between the nodes 2 and subnodes
   $ns duplex-link $n2 $n6 1Mb 10ms DropTail
   $ns duplex-link $n2 $n7 1Mb 10ms DropTail
   $ns duplex-link $n2 $n8 1Mb 10ms DropTail
   $ns duplex-link $n2 $n9 1Mb 10ms DropTail
   $ns duplex-link $n2 $n10 1Mb 10ms DropTail

   #Create a duplex link between the nodes 4 and subnodes
   $ns duplex-link $n4 $n19 1Mb 10ms DropTail
   $ns duplex-link $n4 $n20 1Mb 10ms DropTail
   $ns duplex-link $n4 $n21 1Mb 10ms DropTail
   $ns duplex-link $n4 $n22 1Mb 10ms DropTail

   #Create a duplex link between the nodes 5 and subnodes
   $ns duplex-link $n5 $n15 1Mb 10ms DropTail
   $ns duplex-link $n5 $n16 1Mb 10ms DropTail
   $ns duplex-link $n5 $n17 1Mb 10ms DropTail
   $ns duplex-link $n5 $n18 1Mb 10ms DropTail

   #Create 2Mbps links
   #Create a duplex link between nodes 1 and 3
   $ns duplex-link $n1 $n3 2Mb 20ms DropTail

   #Create a duplex link between nodes 3 and 5
   $ns duplex-link $n3 $n5 2Mb 20ms DropTail



   #Create 4Mbps links
   #Create a duplex link between nodes 2 and 3
   $ns duplex-link $n2 $n3 4Mb 40ms DropTail

   #Create a duplex link between nodes 2 and 4
   $ns duplex-link $n2 $n4 4Mb 40ms DropTail

   #Create a duplex link between nodes 3 and 4
   $ns duplex-link $n3 $n4 4Mb 40ms DropTail

   #Create a UDP agent and attach it to node n4 
   set udp0 [new Agent/UDP]
   $ns attach-agent $n4 $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 set random_ 1
   $cbr0 attach-agent $udp0

   #Create a Null agent (a traffic sink) and attach it to node n8
   set null0 [new Agent/Null]
   $ns attach-agent $n8 $null0

   #Connect CBR to Null agent
   $ns connect $udp0 $null0


   # Create a CBR traffic source and attach it to udp0
   set cbr1 [new Application/Traffic/CBR]
   $cbr1 set packetSize_ 500
   $cbr1 set interval_ 0.005
   $cbr1 set random_ 1
   $cbr1 attach-agent $udp0

   #Create a Null agent (a traffic sink) and attach it to node n18
   set null1 [new Agent/Null]
   $ns attach-agent $n18 $null1


   #Connect CBR to Null agent
   $ns connect $udp0 $null1

   # Create a CBR traffic source and attach it to udp0
   set cbr2 [new Application/Traffic/CBR]
   $cbr2 set packetSize_ 500
   $cbr2 set interval_ 0.005
   $cbr2 set random_ 1
   $cbr2 attach-agent $udp0

   #Create a Null agent (a traffic sink) and attach it to node n21
   set null2 [new Agent/Null]
   $ns attach-agent $n21 $null2

   #Connect CBR to Null agent
   $ns connect $udp0 $null2


   # Create a CBR traffic source and attach it to udp0
   set cbr3 [new Application/Traffic/CBR]
   $cbr3 set packetSize_ 500
   $cbr3 set interval_ 0.005
   $cbr3 set random_ 1
   $cbr3 attach-agent $udp0

   #Create a Null agent (a traffic sink) and attach it to node n10
   set null3 [new Agent/Null]
   $ns attach-agent $n10 $null3


   #Connect CBR to Null agent
   $ns connect $udp0 $null3


   # Create a CBR traffic source and attach it to udp0
   set cbr4 [new Application/Traffic/CBR]
   $cbr4 set packetSize_ 500
   $cbr4 set interval_ 0.005
   $cbr4 set random_ 1
   $cbr4 attach-agent $udp0

   #Create a Null agent (a traffic sink) and attach it to node n11
   set null4 [new Agent/Null]
   $ns attach-agent $n11 $null4


   #Connect CBR to Null agent
   $ns connect $udp0 $null4



   # Create a CBR traffic source and attach it to udp0
   set cbr5 [new Application/Traffic/CBR]
   $cbr5 set packetSize_ 500
   $cbr5 set interval_ 0.005
   $cbr5 set random_ 1
   $cbr5 attach-agent $udp0

   #Create a Null agent (a traffic sink) and attach it to node n16
   set null5 [new Agent/Null]
   $ns attach-agent $n16 $null5


   #Connect CBR to Null agent
   $ns connect $udp0 $null5




   # Create a CBR traffic source and attach it to udp0
   set cbr6 [new Application/Traffic/CBR]
   $cbr6 set packetSize_ 500
   $cbr6 set interval_ 0.005
   $cbr6 set random_ 1
   $cbr6 attach-agent $udp0

   #Create a Null agent (a traffic sink) and attach it to node n13
   set null6 [new Agent/Null]
   $ns attach-agent $n13 $null6


   #Connect CBR to Null agent
   #$ns connect $udp0 $null6


   #Schedule events for the CBR agent
   $ns at 1.0 "$cbr0 start"
   $ns at 1.0 "$cbr1 start"
   $ns at 1.0 "$cbr2 start"
   $ns at 2.0 "$cbr3 start"
   $ns at 2.0 "$cbr4 start"
   $ns at 2.0 "$cbr5 start"
   $ns rtmodel-at 6.0 down $n2 $n4
   $ns rtmodel-at 2.0 up $n2 $n4
   $ns at 10.0 "$cbr0 stop" 
   $ns at 10.0 "$cbr1 stop" 
   $ns at 10.0 "$cbr2 stop" 
   $ns at 2.0 "$cbr3 stop"
   $ns at 2.0 "$cbr4 stop"
   $ns at 2.0 "$cbr5 stop"
   $ns at 10.0 "finish"

   #Run the simulation
   $ns run

现在代码的方式,将提供0个pakets。如果我注释掉除第一个连接之外的所有内容,我会收到数据包。当我取消注释其他任何内容时,我会丢失所有数据包。这是我的awk文件。

cbr.awk

    BEGIN {
       node = 1;
       time1 = 0.0;
       time2 = 0.0;
       num_packets = 0;
    }

    {
       time2 = $2;

       if (time2 - time1 > 0.050) {
          throughput = bytes_counter / (time2 - time1);
          printf("%f \t %f\n", time2, throughput) > "dataset.xls";
          time1 = $2;
          bytes_counter = 0;
       }

       if ($1 == "r" && $4 == node && $5 == "cbr") {
          bytes_counter += $6;
          num_packets++;
       }
    }

    END {
       print("********");
       printf("Total number of packets received: \n%d\n", num_packets);
       print("********");
    }

我很茫然。从字面上看。

0 个答案:

没有答案