COMPUTER NETWORKS BCS502 1. Implement three nodes point – to – point network with duplex links between them. Set the queue size, vary the bandwidth, and find the number of packets dropped. Create simulator object set ns [new Simulator] # Create trace files set tracefile [open out.tr w] set namfile [open out.nam w] $ns trace-all $tracefile $ns namtrace-all $namfile # Create nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] # NAM node positions (optional) $ns at 0.0 "$n0 set X_ 100; $n0 set Y_ 200" $ns at 0.0 "$n1 set X_ 300; $n1 set Y_ 200" $ns at 0.0 "$n2 set X_ 200; $n2 set Y_ 100" # Link parameters set bw "100kb" set delay "10ms" set queue_size 5 # Create duplex links with queue limits $ns duplex-link $n0 $n1 $bw $delay DropTail $ns queue-limit $n0 $n1 $queue_size $ns duplex-link $n1 $n2 $bw $delay DropTail $ns queue-limit $n1 $n2 $queue_size $ns duplex-link $n2 $n0 $bw $delay DropTail $ns queue-limit $n2 $n0 $queue_size # UDP agent and Null sink set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 set null0 [new Agent/Null] $ns attach-agent $n1 $null0 COMPUTER NETWORKS BCS502 $ns connect $udp0 $null0 # CBR traffic set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 512 $cbr0 set interval_ 0.001 $cbr0 attach-agent $udp0 # Schedule traffic start and stop $ns at 1.0 "$cbr0 start" $ns at 20.0 "$cbr0 stop" # Robust finish procedure to count dropped packets safely proc finish {} { global ns tracefilenamfile $ns flush-trace close $tracefile close $namfile set drop_count 0 if {[file exists "out.tr"]} { if {[catch {exec grep "^d" out.tr} result]} { set drop_count 0 } else { set drop_lines [split $result "\n"] set drop_count [expr {[llength $drop_lines] - 1}] if {$drop_count< 0} { set drop_count 0 } } } puts "Simulation finished" puts "Number of dropped packets: $drop_count" exit 0 } # Schedule simulation end $ns at 21.0 "finish" # Run simulation $ns run COMPUTER NETWORKS BCS502 OUTPUT Simulation finished Number of dropped packets: 84672 COMPUTER NETWORKS BCS502 2. Implement transmission of ping messages/trace route over a network topology consisting of 6 nodes and find the number of packets dropped due to congestion. # Create simulator set ns [new Simulator] # Trace files set tracefile [open out.tr w] set namfile [open out.nam w] $ns trace-all $tracefile $ns namtrace-all $namfile # Create nodes for {set i 0} {$i< 6} {incri} { set n($i) [$ns node] } # Link parameters set bw "500Kb" set delay "10ms" set queue_size 10 # Create chain topology links with limited queue for {set i 0} {$i< 5} {incri} { $ns duplex-link $n($i) $n([expr $i + 1]) $bw $delay DropTail $ns queue-limit $n($i) $n([expr $i + 1]) $queue_size } # Set node positions for NAM (optional) $ns at 0.0 "$n(0) set X_ 50; $n(0) set Y_ 100" $ns at 0.0 "$n(1) set X_ 150; $n(1) set Y_ 100" $ns at 0.0 "$n(2) set X_ 250; $n(2) set Y_ 100" $ns at 0.0 "$n(3) set X_ 350; $n(3) set Y_ 100" $ns at 0.0 "$n(4) set X_ 450; $n(4) set Y_ 100" $ns at 0.0 "$n(5) set X_ 550; $n(5) set Y_ 100" # Setup UDP agents and Null sink for ping simulation set udpPing [new Agent/UDP] $ns attach-agent $n(0) $udpPing set nullSink [new Agent/Null] $ns attach-agent $n(5) $nullSink $ns connect $udpPing $nullSink # Create CBR traffic to simulate ping (small packets, moderate interval) COMPUTER NETWORKS BCS502 set cbrPing [new Application/Traffic/CBR] $cbrPing set packetSize_ 64 $cbrPing set interval_ 0.5 ;# send a packet every 0.5 sec $cbrPing attach-agent $udpPing # Schedule ping start/stop $ns at 1.0 "$cbrPing start" $ns at 26.0 "$cbrPing stop" # Additional traffic to cause congestion (between nodes 2 and 3) set udpCbrAgent [new Agent/UDP] $ns attach-agent $n(2) $udpCbrAgent set cbrHeavy [new Application/Traffic/CBR] $cbrHeavy set packetSize_ 512 $cbrHeavy set interval_ 0.01 ;# heavy traffic to cause congestion $cbrHeavy attach-agent $udpCbrAgent set nullHeavy [new Agent/Null] $ns attach-agent $n(3) $nullHeavy $ns connect $udpCbrAgent $nullHeavy $ns at 0.5 "$cbrHeavy start" $ns at 25.0 "$cbrHeavy stop" # Finish procedure to count drops safely proc finish {} { global ns tracefilenamfile $ns flush-trace close $tracefile close $namfile set drop_count 0 if {[file exists "out.tr"]} { if {[catch {exec grep "^d" out.tr} result]} { set drop_count 0 } else { set drop_lines [split $result "\n"] set drop_count [expr {[llength $drop_lines] - 1}] if {$drop_count< 0} { set drop_count 0 } } } puts "Simulation finished" puts "Number of dropped packets due to congestion: $drop_count" COMPUTER NETWORKS BCS502 exit 0 } # Schedule end of simulation $ns at 30.0 "finish" # Run simulation $ns run OUTPUT Simulation finished Number of dropped packets due to congestion: 7 COMPUTER NETWORKS BCS502 3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot congestion window for different source / destination. set ns [new Simulator] # Create nodes for {set i 0} {$i< 5} {incri} { set node($i) [$ns node] } set hub $node(0) ;# Node 0 acts as the hub/switch # Other nodes: node(1), node(2), node(3), node(4) # Link parameters (high bandwidth, low delay to simulate LAN) set bw "100Mb" set delay "0.001ms" set queue_size 50 # Connect each node to hub for {set i 1} {$i<=4} {incri} { $ns duplex-link $hub $node($i) $bw $delay DropTail $ns queue-limit $hub $node($i) $queue_size } # Attach TCP agents and sinks for multiple flows # Flow 1: node 1 -> node 2 set tcp1 [new Agent/TCP] $ns attach-agent $node(1) $tcp1 set sink1 [new Agent/TCPSink] $ns attach-agent $node(2) $sink1 $ns connect $tcp1 $sink1 # Flow 2: node 3 -> node 4 set tcp2 [new Agent/TCP] $ns attach-agent $node(3) $tcp2 set sink2 [new Agent/TCPSink] $ns attach-agent $node(4) $sink2 $ns connect $tcp2 $sink2 # Create FTP apps for these flows set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1 set ftp2 [new Application/FTP] $ftp2 attach-agent $tcp2 COMPUTER NETWORKS BCS502 # Start/stop traffic $ns at 1.0 "$ftp1 start" $ns at 10.0 "$ftp1 stop" $ns at 2.0 "$ftp2 start" $ns at 15.0 "$ftp2 stop" # Trace congestion window changes proc trace_cwnd {agent time val} { puts "$time $val" } $tcp1 trace cwnd_ trace_cwnd $tcp2 trace cwnd_ trace_cwnd # Finish procedure proc finish {} { global ns tracefilenamfile $ns flush-trace close $tracefile close $namfile puts "Simulation finished" exit 0 } # Open trace files set tracefile [open out.tr w] set namfile [open out.nam w] $ns trace-all $tracefile $ns namtrace-all $namfile $ns at 20.0 "finish" # Run $ns run OUTPUT Simulation finished