title: NS2 Network Simulator using TCL - CBR over UDP
body: <p>I am working on a project and I am having problems getting my simulations to work. I have a total of 22 nodes, and when I set up my connections, I am not getting packets on certain links. TO be more specific, I have connectivity and successful packet delievery from node 4 to node 2 and any of node 2's subnodes. When I try to send packets anywhere else, none are delivered. I have looked over my code thoroughly and I can't seem to figure out what i'm doing wrong. Any pointers in the right direction would be appreciated. </p>
  
  <pre><code>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
  </code></pre>
  
  <p>The way the code is now, 0 pakets will be delivered. If I comment out everything except the first connection, I get packets. When I uncomment anything else, I lose all packets. Here is my awk file. </p>
  
  <pre><code>cbr.awk
  
      BEGIN {
         node = 1;
         time1 = 0.0;
         time2 = 0.0;
         num_packets = 0;
      }
  
      {
         time2 = $2;
  
         if (time2 - time1 &gt; 0.050) {
            throughput = bytes_counter / (time2 - time1);
            printf("%f \t %f\n", time2, throughput) &gt; "dataset.xls";
            time1 = $2;
            bytes_counter = 0;
         }
  
         if ($1 == "r" &amp;&amp; $4 == node &amp;&amp; $5 == "cbr") {
            bytes_counter += $6;
            num_packets++;
         }
      }
  
      END {
         print("********");
         printf("Total number of packets received: \n%d\n", num_packets);
         print("********");
      }
  </code></pre>
  
  <p>I'm at a loss. Literally. </p>