1. Implement three-node point-to-point networks with duplex links between them for 2 different topologies (line and ring). Set the queue size, vary the bandwidth, and find the number of packets dropped for various iterations. PROCEDURE: 1. Create a Simulator Object 2. Open trace files 3. Create 3 nodes 4. Create links to connect the nodes 5. Create queue and set size for efficient packet transfer 6. Setup agents (TCP & FTP only) for managing the flow of data packets 7. Schedule events 8. Run the Simulator object Under proc finish{}: a. State the variables as ‘global’ b. Dump & close the trace files c. Execute NAM & [simultaneous task] show the number of packets dropped / received / enqueued / dequeued d. Exit procedure (function) CODE: # Create Simulator set ns [new Simulator] # Open Trace file and NAM file set ntrace [open 3nodes.tr w] set namfile [open 3nodes.nam w] # Trace all events to the trace file $ns trace-all $ntrace $ns namtrace-all $namfile # Procedure to finish simulation and analyze results proc Finish {} { global ns ntrace namfile # Dump all the trace data and close the files $ns flush-trace close $ntrace close $namfile # Execute the nam animation file (optional) exec nam 3nodes.nam & # Calculate packet drops # only grep -> shows an error if count is 0. # using sh -> returns 0 when grep's return value is added logically with 'true'. set num_drops [exec sh -c {grep -c "^d" 3nodes.tr || true}] puts "The number of packet drops is $num_drops." exit 0 } # Create 3 nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] # Label the nodes $n0 label "TCP Source" $n2 label "Sink" # Set the colour $ns color 1 pink # Create Links between nodes # You can modify the bandwidth to observe variation in packet drops $ns duplex-link $n0 $n1 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail # Make the Link Orientation $ns duplex-link-op $n0 $n1 orient right $ns duplex-link-op $n1 $n2 orient right # Set Queue Size # You can modify the queue length as well to observe variation in packet drops $ns queue-limit $n0 $n1 10 $ns queue-limit $n1 $n2 10 # Set up a Transport layer connection. set tcp0 [new Agent/TCP] $ns attach-agent $n0 $tcp0 set sink0 [new Agent/TCPSink] $ns attach-agent $n2 $sink0 $ns connect $tcp0 $sink0 # Set up an Application layer Traffic set ftp0 [new Application/FTP] $ftp0 set packetSize_ 100 $ftp0 set rate_ 1Mb $ftp0 attach-agent $tcp0 $ftp0 set random_ false # Schedule Events $ns at 0.0 "$ftp0 start" $ns at 5.0 "Finish" # Run the Simulation $ns run 2. Simulate a four node point-to-point network, and connect the links as follows: n0-n2, n1-n2 and n2-n3. Apply TCP agent between n0-n3 and UDP nl-n3. Apply relevant applications over TCP and UDP agents changing the parameter and determine the number of packets by TCP and UDP. PROCEDURE: 1. Create a Simulator Object 2. Open trace files 3. Create 4 nodes 4. Create links to connect the nodes 5. Create queue and set size for efficient packet transfer 6. Setup agents (TCP, FTP, UDP & CBR) for managing the flow of data packets 7. Schedule events 8. Run the Simulator object Under proc finish{}: a. State the variables as ‘global’ b. Dump & close the trace files c. Execute NAM & [simultaneous task] show the number of packets dropped / received / enqueued / dequeued d. Exit procedure (function) CODE: # Create a simulator object set ns [new Simulator] # Define different colors for data flows $ns color 1 "Blue" $ns color 2 "Red" # Open the trace both the files set tracefile [open 4nodes.tr w] set namfile [open 4nodes.nam w] $ns trace-all $tracefile $ns namtrace-all $namfile # Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] # Create links between the nodes with bandwidth, delay, and queue type $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 2Mb 10ms DropTail # Set queue size to create potential congestion $ns queue-limit $n0 $n2 10 $ns queue-limit $n1 $n2 10 $ns queue-limit $n2 $n3 10 # Give node positions for NAM $ns duplex-link-op $n0 $n2 orient right $ns duplex-link-op $n1 $n2 orient right $ns duplex-link-op $n2 $n3 orient right # Setup a TCP connection for ping simulation set tcp [new Agent/TCP] $tcp set class_ 1 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink $ns connect $tcp $sink $tcp set fid_ 1 # Setup a FTP over TCP connection to generate traffic set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP # Setup a UDP connection for traceroute simulation set udp [new Agent/UDP] $ns attach-agent $n1 $udp $udp set fid_ 2 set null [new Agent/Null] $ns attach-agent $n3 $null $ns connect $udp $null # Setup a CBR over UDP connection to generate traffic set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false # Define a 'finish' procedure proc finish {} { global ns tracefile namfile $ns flush-trace close $tracefile close $namfile # Execute NAM on the trace file exec nam 4nodes.nam & # Calculate packet drops # only grep -> shows an error if count is 0. # using sh -> returns 0 when grep's return value is added logically with 'true'. set num_drops [exec sh -c {grep -c "^d" 4nodes.tr || true}] puts "The number of packet drops is $num_drops." set num_drops [exec sh -c {grep -c "^d.*tcp" 4nodes.tr || true}] puts "The number of packet drops by FTP is $num_drops." set num_drops [exec sh -c {grep -c "^d.*cbr" 4nodes.tr || true}] puts "The number of packet drops by CBR is $num_drops." exit 0 } # Schedule events $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop" # Call finish after 5 seconds $ns at 5.0 "finish" # Run the simulation $ns run 3. Simulate the transmission of ping messages over a network topology consisting of 6 nodes and find the number of packets dropped due to congestion. PROCEDURE: 1. Create a Simulator Object 2. Open trace files 3. Create 6 nodes 4. Create links to connect the nodes 5. Create queue and set size for efficient packet transfer 6. Setup agents (TCP, FTP, UDP & CBR) for managing the flow of data packets 7. Schedule events 8. Run the Simulator object Under proc finish{}: a. State the variables as ‘global’ b. Dump & close the trace files c. Execute NAM & [simultaneous task] show the number of packets dropped / received / enqueued / dequeued d. Exit procedure (function) CODE: # Create a simulator object set ns [new Simulator] # Define different colors for data flows $ns color 1 "Blue" $ns color 2 "Red" # Open the trace both the files set tracefile [open 6nodes.tr w] set namfile [open 6nodes.nam w] $ns trace-all $tracefile $ns namtrace-all $namfile # Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] # Create links between the nodes with bandwidth, delay, and queue type $ns duplex-link $n0 $n1 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 2Mb 10ms DropTail $ns duplex-link $n3 $n4 2Mb 10ms DropTail $ns duplex-link $n4 $n5 2Mb 10ms DropTail $ns duplex-link $n0 $n3 1Mb 20ms DropTail # Extra link to create alternative paths # Set queue size to create potential congestion $ns queue-limit $n0 $n1 10 $ns queue-limit $n1 $n2 10 $ns queue-limit $n2 $n3 10 $ns queue-limit $n3 $n4 10 $ns queue-limit $n4 $n5 10 $ns queue-limit $n0 $n3 5 # Give node positions for NAM $ns duplex-link-op $n0 $n1 orient right $ns duplex-link-op $n1 $n2 orient right $ns duplex-link-op $n2 $n3 orient right $ns duplex-link-op $n3 $n4 orient right $ns duplex-link-op $n4 $n5 orient right $ns duplex-link-op $n0 $n3 orient down # Setup a TCP connection for ping simulation set tcp [new Agent/TCP] $tcp set class_ 1 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n5 $sink $ns connect $tcp $sink $tcp set fid_ 1 # Setup a FTP over TCP connection to generate traffic set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP # Setup a UDP connection for traceroute simulation set udp [new Agent/UDP] $ns attach-agent $n1 $udp $udp set fid_ 2 set null [new Agent/Null] $ns attach-agent $n4 $null $ns connect $udp $null # Setup a CBR over UDP connection to generate traffic set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false # Define a 'finish' procedure proc finish {} { global ns tracefile namfile $ns flush-trace close $tracefile close $namfile # Execute NAM on the trace file exec nam 6nodes.nam & # Calculate packet drops # only grep -> shows an error if count is 0. # using sh -> returns 0 when grep's return value is added logically with 'true'. set num_drops [exec sh -c {grep -c "^d" 6nodes.tr || true}] puts "The number of packet drops is $num_drops." set num_drops [exec sh -c {grep -c "^d.*tcp" 6nodes.tr || true}] puts "The number of packet drops by FTP is $num_drops." set num_drops [exec sh -c {grep -c "^d.*cbr" 6nodes.tr || true}] puts "The number of packet drops by CBR is $num_drops." exit 0 } # Schedule events $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop" # Call finish after 5 seconds $ns at 5.0 "finish" # Run the simulation $ns run 4. Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and plot congestion windows for different sources / destinations. PROCEDURE: 1. Create a Simulator Object 2. Define colors for data flows 3. Open trace files 4. Create 10 nodes 5. Create duplex links between nodes 6. Setup three TCP/Newreno connections 7. Attach TCP Sink agents 8. Establish TCP connections 9. Setup three FTP applications 10. Schedule FTP start and stop events 11. Define the plotWindow procedure 12. Open three congestion window trace files 13. Schedule congestion window monitoring 14. Schedule the finish procedure 15. Run the Simulator object Under proc finish{}: a. State the variables as ‘global’ b. Calculate packet statistics (dropped / received / enqueued / dequeued) c. Flush and close trace files d. Execute NAM e. Exit procedure (function) Under proc plotWindow{}: a. State the Simulator object as ‘global’ b. Retrieve simulation time and congestion window c. Write congestion window values to trace files d. Schedule repeated monitoring at 0.1-second intervals e. Display congestion window values CODE: # Create a simulator object set ns [new Simulator] # Define different colors for data flows $ns color 1 Blue $ns color 2 Red $ns color 3 Green # Open the trace file set tracefile [open lan.tr w] $ns trace-all $tracefile # Open the NAM trace file set namfile [open lan.nam w] $ns namtrace-all $namfile # Define a 'finish' procedure proc finish {} { global ns tracefile namfile cwnd1 cwnd2 cwnd3 # Calculate packet drops # only grep -> shows an error if count is 0. # using sh -> returns 0 when grep's return value is added logically with 'true'. set num_drops [exec sh -c {grep -c "^d" lan.tr || true}] puts "The number of packets dropped is $num_drops." set num_recd [exec grep -c "^r" lan.tr] puts "The number of packets received is $num_recd." set num_enq [exec grep -c "^+" lan.tr] puts "The number of packets enqueued is $num_enq." set num_deq [exec grep -c "^-" lan.tr] puts "The number of packets dequeued is $num_deq." $ns flush-trace close $tracefile close $namfile close $cwnd1 close $cwnd2 close $cwnd3 exec nam lan.nam & exit 0 } # Set number of nodes set n 10 # Create nodes for {set i 0} {$i < $n} {incr i} { set node($i) [$ns node] } # Create duplex links between nodes for {set i 0} {$i < 9} {incr i} { $ns duplex-link $node($i) $node([expr $i+1]) 10Mb 10ms DropTail } # Setup TCP connections between multiple nodes # TCP connection 1: node 0 -> node 5 set tcp1 [new Agent/TCP/Newreno] $tcp1 set class_ 1 $ns attach-agent $node(0) $tcp1 set sink1 [new Agent/TCPSink] $ns attach-agent $node(5) $sink1 $ns connect $tcp1 $sink1 # TCP connection 2: node 2 -> node 7 set tcp2 [new Agent/TCP/Newreno] $tcp2 set class_ 2 $ns attach-agent $node(2) $tcp2 set sink2 [new Agent/TCPSink] $ns attach-agent $node(7) $sink2 $ns connect $tcp2 $sink2 # TCP connection 3: node 4 -> node 9 set tcp3 [new Agent/TCP/Newreno] $tcp3 set class_ 3 $ns attach-agent $node(4) $tcp3 set sink3 [new Agent/TCPSink] $ns attach-agent $node(9) $sink3 $ns connect $tcp3 $sink3 # Setup FTP applications over TCP connections set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1 set ftp2 [new Application/FTP] $ftp2 attach-agent $tcp2 set ftp3 [new Application/FTP] $ftp3 attach-agent $tcp3 # Schedule events $ns at 0.1 "$ftp1 start" $ns at 0.2 "$ftp2 start" $ns at 0.3 "$ftp3 start" $ns at 10.0 "$ftp1 stop" $ns at 10.0 "$ftp2 stop" $ns at 10.0 "$ftp3 stop" $ns at 10.5 "finish" # Procedure to plot congestion window proc plotWindow {tcpSource outfile} { global ns set now [$ns now] set cwnd [$tcpSource set cwnd_] puts $outfile "$now $cwnd" $ns at [expr $now+0.1] "plotWindow $tcpSource $outfile" puts "Time: $now, Congestion Window: $cwnd" } # Open files for writing congestion window data set cwnd1 [open cwnd1.tr w] set cwnd2 [open cwnd2.tr w] set cwnd3 [open cwnd3.tr w] $ns at 0.0 "plotWindow $tcp1 $cwnd1" $ns at 0.0 "plotWindow $tcp2 $cwnd2" $ns at 0.0 "plotWindow $tcp3 $cwnd3" # Run the simulation $ns run 5. Simulate simple ESS and transmitting nodes in wire-less LAN by simulation and determine the performance with respect to transmission of packets. PROCEDURE: 1. Create a Simulator Object 2. Open trace files 3. Define topology area 4. Create a Topography Object 5. Configure wireless parameters 6. Create God Object 7. Create 5 wireless nodes 8. Set node positions 9. Define node mobility 10. Setup UDP and Null agents 11. Establish connection 12. Setup CBR traffic 13. Schedule events 14. Run the Simulator object Under proc finish{}: a. State the variables as ‘global’ b. Flush and close trace files c. Execute NAM & display packet statistics (dropped / received / enqueued / dequeued) d. Exit procedure (function) CODE: # Create the simulator set ns [new Simulator] # Setup for NAM and trace set tracefile [open ess.tr w] $ns trace-all $tracefile set namfile [open ess.nam w] $ns namtrace-all-wireless $namfile 500 500 # Define topology area set val(x) 500 set val(y) 500 # Finish procedure proc finish {} { global ns tracefile namfile $ns flush-trace close $tracefile close $namfile exec nam ess.nam & # Calculate packet drops # only grep -> shows an error if count is 0. # using sh -> returns 0 when grep's return value is added logically with 'true'. set num_drops [exec sh -c {grep -c "^d" ess.tr || true}] puts "The number of packets dropped is $num_drops." set num_recd [exec grep -c "^r" ess.tr] puts "The number of packets received is $num_recd." set num_enq [exec sh -c {grep -c "^+" ess.tr || true}] puts "The number of packets enqueued is $num_enq." set num_deq [exec sh -c {grep -c "^-" ess.tr || true}] puts "The number of packets dequeued is $num_deq." exit 0 } # Set up topology area set topo [new Topography] $topo load_flatgrid $val(x) $val(y) # Configure wireless settings $ns node-config -adhocRouting DSDV \ -llType LL \ -macType Mac/802_11 \ -ifqType Queue/DropTail \ -ifqLen 50 \ -antType Antenna/OmniAntenna \ -propType Propagation/TwoRayGround \ -phyType Phy/WirelessPhy \ -channelType Channel/WirelessChannel \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace ON # Create the God Object create-god 5 # Create 5 nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] # Set node positions $n0 set X_ 100; $n0 set Y_ 100; $n0 set Z_ 0 $n1 set X_ 150; $n1 set Y_ 200; $n1 set Z_ 0 $n2 set X_ 200; $n2 set Y_ 250; $n2 set Z_ 0 $n3 set X_ 300; $n3 set Y_ 200; $n3 set Z_ 0 $n4 set X_ 400; $n4 set Y_ 100; $n4 set Z_ 0 # Define base station (AP node) $ns initial_node_pos $n0 30 $ns initial_node_pos $n1 30 $ns initial_node_pos $n2 30 $ns initial_node_pos $n3 30 $ns initial_node_pos $n4 30 # Set node movement using setdest: node setdest x y speed $ns at 1.0 "$n1 setdest 200 100 10.0" $ns at 1.0 "$n2 setdest 250 250 5.0" $ns at 1.5 "$n3 setdest 300 300 3.0" $ns at 2.0 "$n4 setdest 100 300 7.0" $ns at 2.5 "$n0 setdest 350 100 2.5" # Connect n0 (AP) to n4 (mobile) using UDP set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n4 $null $ns connect $udp $null # Setup CBR traffic over UDP set cbr [new Application/Traffic/CBR] $cbr set packetSize_ 512 $cbr set interval_ 0.1 $cbr attach-agent $udp # Schedule traffic $ns at 1.0 "$cbr start" $ns at 5.0 "$cbr stop" $ns at 6.0 "finish" # Run the simulation $ns run 6. Write a program for error detecting code using CRC-CCITT (16-bits). PROCEDURE: 1. initialize variable crc = 0xFFFF and variable polynomial = 0x1021 2. for each bit in the input data: a. extract msb of crc (shift bits to the right by 15 positions) b. shift crc bits to the left by 1 position c. if msb does not match the current data bit: XOR polynomial over crc 3. return crc CODE: # defining a function for crc-ccitt calculation def crc_ccitt(data): crc = 0xFFFF polynomial = 0x1021 for bit in data: # XOR the input bit with the MSB of CRC msb = (crc >> 15) & 1 crc = (crc << 1) & 0xFFFF if msb ^ int(bit): crc ^= polynomial return crc # Sender side data = input("Enter data bits: ") crc = crc_ccitt(data) # Convert CRC to 16-bit binary crc_bits = format(crc, '016b') print("CRC:", crc_bits) transmitted_data = data + crc_bits print("Transmitted data:", transmitted_data) # Receiver side received_data = input("\nEnter received data bits: ") if crc_ccitt(received_data) == 0: print("No error detected.") else: print("Error detected!") 7. Write a program for congestion control using a leaky bucket algorithm. PROCEDURE: 1. initialize number of queries, bucket capacity, i/p o/p packet size 2. for n number of queries, repeat: a. check the remaining storage size b. if i/p size <= remaining storage size: accommodate packet in the bucket c. else: print "overflow occurred by (i/p size - remaining storage size)" d. display current storage used e. release final packet CODE: # taking inputs from the user bucket_size = int(input("Enter bucket size: ")) output_rate = int(input("Enter output rate: ")) n = int(input("Enter number of packets: ")) bucket = 0 for i in range(n): packet = int(input(f"Enter size of packet {i + 1}: ")) # Check if packet can fit in the bucket if bucket + packet > bucket_size: print("Packet dropped! Bucket overflow.") else: bucket += packet print(f"Packet added. Current bucket size: {bucket}") # Send packets at fixed output rate if bucket > 0: sent = min(bucket, output_rate) bucket -= sent print(f"Sent: {sent}") print(f"Remaining in bucket: {bucket}") print()