This document contains code for simulating different network topologies and routing algorithms using the ns-2 network simulator. It defines procedures for creating bus, ring and star topologies with 5, 6 and 6 nodes respectively. It also simulates stop-and-wait and sliding window protocols, and distance vector and link state routing algorithms on a 12 node network. The simulations generate nam trace files and trace data for visualization and analysis.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
15 views
Network Programs1
This document contains code for simulating different network topologies and routing algorithms using the ns-2 network simulator. It defines procedures for creating bus, ring and star topologies with 5, 6 and 6 nodes respectively. It also simulates stop-and-wait and sliding window protocols, and distance vector and link state routing algorithms on a 12 node network. The simulations generate nam trace files and trace data for visualization and analysis.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17
BUS TOPOLOGY
set ns [new Simulator]
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 file close $nf #Executenam on the trace file exec nam out.nam & exit 0 } #Create five nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] #Create Lan between the nodes set lan0 [$ns newLan "$n0 $n1 $n2 $n3 $n4" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Cd Channel] #Create a TCP agent and attach it to node n0 set tcp0 [new Agent/TCP] $tcp0 set class_ 1 $ns attach-agent $n1 $tcp0 #Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3 set sink0 [new Agent/TCPSink] $ns attach-agent $n3 $sink0 #Connect the traffic sources with the traffic sink $ns connect $tcp0 $sink0 # Create a CBR traffic source and attach it to tcp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.01 $cbr0 attach-agent $tcp0 #Schedule events for the CBR agents $ns at 0.5 "$cbr0 start" $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 RING TOPOLOGY set ns [new Simulator] 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 file close $nf #Executenam on the trace file exec nam out.nam & exit0 } #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 $ns duplex-link $n0 $n1 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n2 $n3 1Mb 10ms DropTail $ns duplex-link $n3 $n4 1Mb 10ms DropTail $ns duplex-link $n4 $n5 1Mb 10ms DropTail $ns duplex-link $n5 $n0 1Mb 10ms DropTail #Create a TCP agent and attach it to node n0 set tcp0 [new Agent/TCP] $tcp0 set class_ 1 $ns attach-agent $n1 $tcp0 #Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3 set sink0 [new Agent/TCPSink] $ns attach-agent $n3 $sink0 #Connect the traffic sources with the traffic sink $ns connect $tcp0 $sink0 # Create a CBR traffic source and attach it to tcp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.01 $cbr0 attach-agent $tcp0 #Schedule events for the CBR agents $ns at 0.5 "$cbr0 start" $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 STAR TOPOLOGY set ns [new Simulator] set nf [open out.nam w] $ns namtrace-all $nf proc finish {} { global ns nf $ns flush-trace close $nf exec nam out.nam & exit0 } set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] $n0 shape square $ns duplex-link $n0 $n1 1Mb 10ms DropTail $ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n0 $n3 1Mb 10ms DropTail $ns duplex-link $n0 $n4 1Mb 10ms DropTail $ns duplex-link $n0 $n5 1Mb 10ms DropTail set tcp0 [new Agent/TCP] $tcp0 set class_ 1 $ns attach-agent $n1 $tcp0 set sink0 [new Agent/TCPSink] $ns attach-agent $n3 $sink0 $ns connect $tcp0 $sink0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.01 $cbr0 attach-agent $tcp0 $ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop" $ns at 5.0 "finish" $ns run SIMULATION OF STOP AND WAIT PROTOCOL # stop and wait protocol in normal situation # features : labeling, annotation, nam-graph, and window size monitoring set ns [new Simulator] set n0 [$ns node] set n1 [$ns node] $ns at 0.0 "$n0 label Sender" $ns at 0.0 "$n1 label Receiver" set nf [open stop.nam w] $ns namtrace-all $nf set f [open stop.tr w] $ns trace-all $f $ns duplex-link $n0 $n1 0.2Mb 200ms DropTail $ns duplex-link-op $n0 $n1 orient right $ns queue-limit $n0 $n1 10 Agent/TCP set nam_tracevar_ true set tcp [new Agent/TCP] $tcp set window_ 1 $tcp set maxcwnd_ 1 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n1 $sink $ns connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns add-agent-trace $tcp tcp $ns monitor-agent-trace $tcp $tcp tracevar cwnd_ $ns at 0.1 "$ftp start" $ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink" $ns at 3.5 "finish" $ns at 0.0 "$ns trace-annotate \"Stop and Wait with normal operation\"" $ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\"" $ns at 0.11 "$ns trace-annotate \"Send Packet_0\"" $ns at 0.35 "$ns trace-annotate \"Receive Ack_0\"" $ns at 0.56 "$ns trace-annotate \"Send Packet_1\"" $ns at 0.79 "$ns trace-annotate \"Receive Ack_1\"" $ns at 0.99 "$ns trace-annotate \"Send Packet_2\"" $ns at 1.23 "$ns trace-annotate \"Receive Ack_2 \"" $ns at 1.43 "$ns trace-annotate \"Send Packet_3\"" $ns at 1.67 "$ns trace-annotate \"Receive Ack_3\"" $ns at 1.88 "$ns trace-annotate \"Send Packet_4\"" $ns at 2.11 "$ns trace-annotate \"Receive Ack_4\"" $ns at 2.32 "$ns trace-annotate \"Send Packet_5\"" $ns at 2.55 "$ns trace-annotate \"Receive Ack_5 \"" $ns at 2.75 "$ns trace-annotate \"Send Packet_6\"" $ns at 2.99 "$ns trace-annotate \"Receive Ack_6\"" $ns at 3.1 "$ns trace-annotate \"FTP stops\"" proc finish {} { global ns nf $ns flush-trace close $nf puts "running nam..." exec nam stop.nam & exit 0 } $ns run SLIDING WINDOW PROTOCOL # sliding window mechanism with some features # such as labeling, annotation, nam-graph, and window size monitoring set ns [new Simulator] set n0 [$ns node] set n1 [$ns node] $ns at 0.0 "$n0 label Sender" $ns at 0.0 "$n1 label Receiver" set nf [open sliding.nam w] $ns namtrace-all $nf set f [open sliding.tr w] $ns trace-all $f $ns duplex-link $n0 $n1 0.2Mb 200ms DropTail $ns duplex-link-op $n0 $n1 orient right $ns queue-limit $n0 $n1 10 Agent/TCP set nam_tracevar_ true set tcp [new Agent/TCP] $tcp set windowInit_ 4 $tcp set maxcwnd_ 4 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n1 $sink $ns connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns add-agent-trace $tcp tcp $ns monitor-agent-trace $tcp $tcp tracevar cwnd_ $ns at 0.1 "$ftp start" $ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink" $ns at 3.5 "finish" $ns at 0.0 "$ns trace-annotate \"Sliding Window with window size 4 (normal operation)\"" $ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\"" $ns at 0.11 "$ns trace-annotate \"Send Packet_0,1,2,3\"" $ns at 0.34 "$ns trace-annotate \"Receive Ack_0,1,2,3\"" $ns at 0.56 "$ns trace-annotate \"Send Packet_4,5,6,7\"" $ns at 0.79 "$ns trace-annotate \"Receive Ack_4,5,6,7\"" $ns at 0.99 "$ns trace-annotate \"Send Packet_8,9,10,11\"" $ns at 1.23 "$ns trace-annotate \"Receive Ack_8,9,10,11 \"" $ns at 1.43 "$ns trace-annotate \"Send Packet_12,13,14,15\"" $ns at 1.67 "$ns trace-annotate \"Receive Ack_12,13,14,15\"" $ns at 1.88 "$ns trace-annotate \"Send Packet_16,17,18,19\"" $ns at 2.11 "$ns trace-annotate \"Receive Ack_16,17,18,19\"" $ns at 2.32 "$ns trace-annotate \"Send Packet_20,21,22,23\"" $ns at 2.56 "$ns trace-annotate \"Receive Ack_24,25,26,27\"" $ns at 2.76 "$ns trace-annotate \"Send Packet_28,29,30,31\"" $ns at 3.00 "$ns trace-annotate \"Receive Ack_28\"" $ns at 3.1 "$ns trace-annotate \"FTP stops\"" proc finish {} { global ns $ns flush-trace # close $nf puts "running nam..." exec nam sliding.nam & exit 0 } $ns run SIMULATION OF DISTANCE VECTOR ROUTING ALGORITHM set ns [new Simulator] set nr [open thro.tr w] $ns trace-all $nr set nf [open thro.nam w] $ns namtrace-all $nf proc finish { } { global ns nr nf $ns flush-trace close $nf close $nr exec nam thro.nam & exit 0 } for { set i 0 } { $i < 12} { incr i 1 } { set n($i) [$ns node]} for {set i 0} {$i < 8} {incr i} { $ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail } $ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail $ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail $ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail $ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail $ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail $ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail set udp0 [new Agent/UDP] $ns attach-agent $n(0) $udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0 set null0 [new Agent/Null] $ns attach-agent $n(5) $null0 $ns connect $udp0 $null0 set udp1 [new Agent/UDP] $ns attach-agent $n(1) $udp1 set cbr1 [new Application/Traffic/CBR] $cbr1 set packetSize_ 500 $cbr1 set interval_ 0.005 $cbr1 attach-agent $udp1 set null0 [new Agent/Null] $ns attach-agent $n(5) $null0 $ns connect $udp1 $null0 $ns rtproto DV $ns rtmodel-at 10.0 down $n(11) $n(5) $ns rtmodel-at 15.0 down $n(7) $n(6) $ns rtmodel-at 30.0 up $n(11) $n(5) $ns rtmodel-at 20.0 up $n(7) $n(6) $udp0 set fid_ 1 $udp1 set fid_ 2 $ns color 1 Red $ns color 2 Green $ns at 1.0 "$cbr0 start" $ns at 2.0 "$cbr1 start" $ns at 45 "finish" $ns run SIMULATION OF LINK STATE ROUTING ALGORITHM set ns [new Simulator] set nr [open thro.tr w] $ns trace-all $nr set nf [open thro.nam w] $ns namtrace-all $nf proc finish { } { global ns nr nf $ns flush-trace close $nf close $nr exec nam thro.nam & exit 0 } for { set i 0 } { $i < 12} { incr i 1 } { set n($i) [$ns node]} for {set i 0} {$i < 8} {incr i} { $ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail } $ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail $ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail $ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail $ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail $ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail $ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail set udp0 [new Agent/UDP] $ns attach-agent $n(0) $udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0 set null0 [new Agent/Null] $ns attach-agent $n(5) $null0 $ns connect $udp0 $null0 set udp1 [new Agent/UDP] $ns attach-agent $n(1) $udp1 set cbr1 [new Application/Traffic/CBR] $cbr1 set packetSize_ 500 $cbr1 set interval_ 0.005 $cbr1 attach-agent $udp1 set null0 [new Agent/Null] $ns attach-agent $n(5) $null0 $ns connect $udp1 $null0 $ns rtproto LS $ns rtmodel-at 10.0 down $n(11) $n(5) $ns rtmodel-at 15.0 down $n(7) $n(6) $ns rtmodel-at 30.0 up $n(11) $n(5) $ns rtmodel-at 20.0 up $n(7) $n(6) $udp0 set fid_ 1 $udp1 set fid_ 2 $ns color 1 Red $ns color 2 Green $ns at 1.0 "$cbr0 start" $ns at 2.0 "$cbr1 start" $ns at 45 "finish" $ns run STUDY OF HIGH-LEVEL DATA LINK CONTROL PROTOCOL(HDLC)
set ns [new Simulator]
#Tell the simulator to use dynamic routing $ns rtproto DV $ns macType Mac/Sat/UnslottedAloha #Open the nam trace file set nf [open aloha.nam w] $ns namtrace-all $nf #Open the output files set f0 [open aloha.tr w] $ns trace-all $f0 #Define a finish procedure proc finish {} { global ns f0 nf $ns flush-trace #Close the trace file close $f0 close $nf exec nam aloha.nam & exit 0 } # 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 duplex links between nodes with bandwidth and distance $ns duplex-link $n0 $n4 1Mb 50ms DropTail $ns duplex-link $n1 $n4 1Mb 50ms DropTail $ns duplex-link $n2 $n5 1Mb 1ms DropTail $ns duplex-link $n3 $n5 1Mb 1ms DropTail $ns duplex-link $n4 $n5 1Mb 50ms DropTail $ns duplex-link $n2 $n3 1Mb 50ms DropTail # Create a duplex link between nodes 4 and 5 as queue position $ns duplex-link-op $n4 $n5 queuePos 0.5 #Create a UDP agent and attach it to node n(0) 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 Null agent (a traffic sink) and attach it to node n(2) set null0 [new Agent/Null] $ns attach-agent $n2 $null0 #Connect the traffic source with the traffic sink $ns connect $udp0 $null0 #Schedule events for the CBR agent and the network dynamics $ns at 0.5 "$cbr0 start" $ns rtmodel-at 1.0 down $n5 $n2 $ns rtmodel-at 2.0 up $n5 $n2 $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 DATA ENCRYPTION AND DECRYPTION # include<studio.h> #include<conio.h> void main ( ) { static int s, i, k,n,c[100]; printf(“\n program 1: encryption and 2. decryption”); scanf (“%d”, &s); switch (s) { case 1: printf(“enter the key value:”); scanf(“%d”, &k); printf(“enter the length of text:”); scanf(“%d”, &n); printf(“enter the data to be encrypted:”); for (i=0;i<=n;i++) scanf(“%c”, &c[i]); for (i=0;i<=n;i++) { c[i]=c[i]+k; if (c[i]>90) c[i]=c[i]-26; } printf(“encrypted data”); for (i=1;i<=n;i++) printf(“%c”, c[i]); break; case 2: printf(“enter the key value:”); scanf(“%d”, &k); printf(“enter the length of text:”); scanf(“%d”, &n); printf(“enter the data to be decrypted:”); for (i=0;i<=n;i++) scanf(“%c”, &c[i]); for (i=0;i<=n;i++) { c[i]=c[i]-k; if (c[i]<=n;i++) printf(“%c”, c[i]); break; case 3: break; getch (); } } PERFORMANCE ANALYSIS OF CSMA/CA
set ns [new Simulator]
#Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the Trace files set file1 [open out.tr w] set winfile [open WinFile w] $ns trace-all $file1 #Open the NAM trace file set file2 [open out.nam w] $ns namtrace-all $file2 #Define a 'finish' procedure proc finish {} { global ns file1 file2 $ns flush-trace close $file1 close $file2 exec nam out.nam & exit 0 } #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] $n1 color red $n1 shape box #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns simplex-link $n2 $n3 0.3Mb 100ms DropTail $ns simplex-link $n3 $n2 0.3Mb 100ms DropTail set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Ca Channel] #Setup a TCP connection set tcp [new Agent/TCP/Newreno] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink/DelAck] $ns attach-agent $n4 $sink $ns connect $tcp $sink $tcp set fid_ 1 $tcp set window_ 8000 $tcp set packetSize_ 552 #Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP #Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n5 $null $ns connect $udp $null $udp set fid_ 2 #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 0.01mb $cbr set random_ false $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 124.0 "$ftp stop" $ns at 124.5 "$cbr stop" # next procedure gets two arguments: the name of the # tcp source node, will be called here "tcp", # and the name of output file. proc plotWindow {tcpSource file} { global ns set time 0.1 set now [$ns now] set cwnd [$tcpSource set cwnd_] set wnd [$tcpSource set window_] puts $file "$now $cwnd" $ns at [expr $now+$time] "plotWindow $tcpSource $file" } $ns at 0.1 "plotWindow $tcp $winfile" $ns at 5 "$ns trace-annotate \"packet drop\"" # PPP $ns at 125.0 "finish" $ns run PERFORMANCE ANALYSIS OF CSMA/CD
set ns [new Simulator]
#Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the Trace files set file1 [open out.tr w] set winfile [open WinFile w] $ns trace-all $file1 #Open the NAM trace file set file2 [open out.nam w] $ns namtrace-all $file2 #Define a 'finish' procedure proc finish {} { global ns file1 file2 $ns flush-trace close $file1 close $file2 exec nam out.nam & exit 0 } #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] $n1 color red $n1 shape box #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns simplex-link $n2 $n3 0.3Mb 100ms DropTail $ns simplex-link $n3 $n2 0.3Mb 100ms DropTail set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Cd Channel] #Setup a TCP connection set tcp [new Agent/TCP/Newreno] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink/DelAck] $ns attach-agent $n4 $sink $ns connect $tcp $sink $tcp set fid_ 1 $tcp set window_ 8000 $tcp set packetSize_ 552 #Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP #Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n5 $null $ns connect $udp $null $udp set fid_ 2 #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 0.01mb $cbr set random_ false $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 124.0 "$ftp stop" $ns at 124.5 "$cbr stop" # next procedure gets two arguments: the name of the # tcp source node, will be called here "tcp", # and the name of output file. proc plotWindow {tcpSource file} { global ns set time 0.1 set now [$ns now] set cwnd [$tcpSource set cwnd_] set wnd [$tcpSource set window_] puts $file "$now $cwnd" $ns at [expr $now+$time] "plotWindow $tcpSource $file" } $ns at 0.1 "plotWindow $tcp $winfile" $ns at 5 "$ns trace-annotate \"packet drop\"" # PPP $ns at 125.0 "finish" $ns run
CCNP Enterprise Design ENSLD 300-420 Official Cert Guide: Designing Cisco Enterprise Networks (Certification Guide) 1st Edition Anthony Bruno - Instantly access the full ebook content in just a few seconds