Sample Program on NS2/NS3

September 30, 2017 | Author: Radhika Krishnan | Category: Port (Computer Networking), Telecommunications, Data Transmission, Computer Networking, Technology
Share Embed Donate


Short Description

NS2 program...

Description

1.

Simulate three nodes point-to-point networks with a duplex link between them. Set the queue size and vary the bandwidth and find the number of packets dropped.

#Create simulator set ns [new Simulator]

set ntrace [open 1.tr w] $ns trace-all $ntrace set file2 [open out.nam w] $ns namtrace-all $file2

proc finish {} { global ns ntrace $ns flush-trace close $ntrace exit 0 }

#create node set n0 [$ns node] set n1 [$ns node] set n2 [$ns node]

#create link $ns duplex-link $n0 $n1 10Mb 10ms DropTail $ns duplex-link $n1 $n2 10Mb 10ms DropTail

#Set Queue Size $ns queue-limit $n0 $n1 10 $ns queue-limit $n1 $n2 10

#setup tcp connection set tcp0 [new Agent/TCP] $ns attach-agent $n0 $tcp0

#set sink to node set sink [new Agent/TCPSink] $ns attach-agent $n2 $sink

#connect tcp src and sink $ns connect $tcp0 $sink

$tcp0 set window_ 1

set cbr [new Application/Traffic/CBR] $cbr attach-agent $tcp0 $cbr set type_ CBR $cbr set packetSize_ 100 $cbr set rate_ 0.1Mb $cbr set random_ false $ns at 0.0 "$cbr start" $ns at 5.0 "finish" $ns run -----------------------------------------------------------------------------------------------------------------

2. Apply TCP agent between n0 to n3 and UDP n1 to n3. Apply relevant applications over TCP and UDP agents changing the parameters and determine the number of packets sent by TCP/UDP. set ns [new Simulator]

# Open the Trace file set ntrace [open out.tr w] $ns trace-all $ntrace # Finish procedure proc finish {} { global ns ntrace $ns flush-trace close $ntrace exit 0 }

# Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]

# 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.07Mb 20ms DropTail $ns simplex-link $n3 $n2 0.07Mb 20ms DropTail

# Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 10

# Monitor the queue for link (n2-n3) $ns simplex-link-op $n2 $n3 queuePos 0.5

# Setup a TCP connection set tcp [new Agent/TCP/Newreno] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink/DelAck] $ns attach-agent $n3 $sink $ns connect $tcp $sink $tcp set fid_ 1

# 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 $n3 $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 packetSize_ 1000 $cbr set rate_ 0.01Mb $cbr set random_ false

$ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 24.0 "$ftp stop" $ns at 24.5 "$cbr stop"

$ns at 25.5.0 "finish"

$ns run

3.Simulate the different type of internet traffic such as FTP and TELNET over a network and analyze the throughput. #Create Simulator set ns [new Simulator] #Open Trace and NAM Trace File set ntrace [open prog3.tr w] $ns trace-all $ntrace set namfile [open prog3.nam w] $ns namtrace-all $namfile #Finish Procedure proc Finish {} { global ns ntrace namfile #Dump all trace data and close the files $ns flush-trace close $ntrace close $namfile #Execute the nam animation file

exec nam prog3.nam & #Calculate throughput = (number of packets received/time taken for simulation) set numTcp [exec grep "^r" prog3.tr | grep "tcp" | tail -n 1 | cut -d " " -f 6] set TcpSize [exec grep "^r" prog3.tr | grep -c "tcp"] set tcpTime 24.0 set numUdp [exec grep "^r" prog3.tr | grep "udp" | tail -n 1 | cut -d " " -f 6] set UdpSize [exec grep "^r" prog3.tr | grep -c "udp"] set udpTime 23.9 puts "The throughput of FTP is " puts "[expr ($numTcp*$TcpSize)/$tcpTime] bytes per second" puts "The throughput of Telnet is " puts "[expr ($numUdp*$UdpSize)/$udpTime] bytes per second" exit 0 }

#Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] #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 1Mb 10ms DropTail $ns simplex-link $n3 $n2 1Mb 10ms DropTail

#Set queue size and Monitor the queue $ns queue-limit $n0 $n2 10 $ns simplex-link-op $n0 $n2 queuePos 0.5 #Set TCP Connection between n0 and n3 set tcp0 [new Agent/TCP] $ns attach-agent $n0 $tcp0 set sink0 [new Agent/TCPSink] $ns attach-agent $n3 $sink0 $ns connect $tcp0 $sink0 #Attach FTP Application over TCP set ftp0 [new Application/FTP] $ftp0 attach-agent $tcp0 $ftp0 set type_ FTP #Set UDP Connection between n1 and n3 set udp0 [new Agent/UDP] $ns attach-agent $n1 $udp0 set null0 [new Agent/Null] $ns attach-agent $n3 $null0 $ns connect $udp0 $null0 #Attach Telnet Application over UDP set telnet [new Application/Telnet] $telnet attach-agent $udp0 $telnet set type_ Telnet #Schedule Events $ns at 0.1 "$telnet start" $ns at 0.5 "$ftp0 start" $ns at 24.0 "$telnet stop" $ns at 24.5 "$ftp0 stop" $ns at 25.0 "Finish"

#Run Simulation $ns run

4. Simulate a transmission of ping message over a network topology consisting of 6 nodes and find the number of packets dropped due to congestion. #Create a simulator object set ns [new Simulator] #Open a trace file set nf [open exp4.nam w] $ns namtrace-all $nf set nd [open exp4.tr w] $ns trace-all $nd $ns color 1 Red $ns color 2 Green #Define a 'finish' procedure proc finish {} { global ns nf nd $ns flush-trace close $nf close $nd exec nam exp4.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] #Connect the nodes with two links $ns duplex-link $n0 $n1 0.1Mb 10ms DropTail $ns duplex-link $n1 $n2 0.1Mb 10ms DropTail $ns duplex-link $n2 $n3 0.1Mb 10ms DropTail $ns duplex-link $n3 $n4 0.1Mb 10ms DropTail $ns duplex-link $n4 $n5 0.1Mb 10ms DropTail $ns duplex-link-op $n0 $n1 orient right $ns duplex-link-op $n1 $n2 orient right $ns duplex-link-op $n2 $n3 orient down $ns duplex-link-op $n3 $n4 orient left $ns duplex-link-op $n4 $n5 orient left

#Define a 'recv' function for the class 'Agent/Ping' Agent/Ping instproc recv {from rtt} { $self instvar node_ puts "node [$node_ id] received ping answer from \ #$from with round-trip-time $rtt ms." } #Create two ping agents and attach them to the nodes n0 and n2 set p0 [new Agent/Ping] $ns attach-agent $n0 $p0 $p0 set fid_ 1 set p1 [new Agent/Ping]

$ns attach-agent $n5 $p1 $p1 set fid_ 2 #Connect the two agents $ns connect $p0 $p1 #Schedule events $ns at 0.2 "$p0 send" $ns at 0.4 "$p1 send" $ns at 0.6 "$p0 send" $ns at 0.8 "$p1 send" $ns at 1.0 "finish" #Run the simulation $ns run

5.Simulate an Ethernet LAN using n nodes (6 ), change error rate and data rate and compare the throughput. #Create Simulator set ns [new Simulator]

#Use colors to differentiate the traffic $ns color 1 Blue $ns color 2 Red

#Open trace and NAM trace file set ntrace [open prog5.tr w]

$ns trace-all $ntrace set namfile [open prog5.nam w] $ns namtrace-all $namfile

#Finish Procedure proc Finish {} { global ns ntrace namfile

#Dump all trace data and close the files $ns flush-trace close $ntrace close $namfile

#Execute the nam animation file exec nam prog5.nam &

#Calculate the throughput = (number of packets received/time taken for simulation) set TcpSize [exec grep "^r" prog5.tr | grep "tcp" | tail -n 1 | cut -d " " -f 6] set numTcp [exec grep "^r" prog5.tr | grep -c "tcp"] set tcpTime 123.0 set UdpSize [exec grep "^r" prog5.tr | grep "cbr" | tail -n 1 | cut -d " " -f 6] set numUdp [exec grep "^r" prog5.tr | grep -c "cbr"] set udpTime 124.4 puts "The throughput of FTP is" puts "[expr ($numTcp*$TcpSize)/$tcpTime] bytes per second" puts "The throughput of CBR is" puts "[expr ($numUdp*$UdpSize)/$udpTime] bytes per second" exit 0 }

#Create 6 nodes for {set i 0} {$i < 6} {incr i} { set n($i) [$ns node] }

#Create duplex links between the nodes $ns duplex-link $n(0) $n(2) 2Mb 10ms DropTail $ns duplex-link $n(1) $n(2) 2Mb 10ms DropTail $ns simplex-link $n(2) $n(3) 0.3Mb 100ms DropTail $ns simplex-link $n(3) $n(2) 0.3Mb 100ms DropTail

#Node n(3), n(4) and n(5) are considered in a LAN set lan [$ns newLan "$n(3) $n(4) $n(5)" 0.5Mb 40ms LL Queue/DropTail MAC/802_3 Channel]

#Orientation to the nodes $ns duplex-link-op $n(0) $n(2) orient right-down $ns duplex-link-op $n(1) $n(2) orient right-up $ns simplex-link-op $n(2) $n(3) orient right

#Setup queue between n(2) and n(3) and monitor the queue $ns queue-limit $n(2) $n(3) 20 $ns simplex-link-op $n(2) $n(3) queuePos 0.5

#Set error model on link n(2) and n(3) and insert the error model set loss_module [new ErrorModel] $loss_module ranvar [new RandomVariable/Uniform] $loss_module drop-target [new Agent/Null] $ns lossmodel $loss_module $n(2) $n(3)

#Setup TCP Connection between n(0) and n(4) set tcp0 [new Agent/TCP/Newreno] $tcp0 set fid_ 1 $tcp0 set window_ 8000 $tcp0 set packetSize_ 552 $ns attach-agent $n(0) $tcp0 set sink0 [new Agent/TCPSink/DelAck] $ns attach-agent $n(4) $sink0 $ns connect $tcp0 $sink0

#Apply FTP Application over TCP set ftp0 [new Application/FTP] $ftp0 set type_ FTP $ftp0 attach-agent $tcp0

#Setup UDP Connection between n(1) and n(5) set udp0 [new Agent/UDP] $udp0 set fid_ 2 $ns attach-agent $n(1) $udp0 set null0 [new Agent/Null]

$ns attach-agent $n(5) $null0 $ns connect $udp0 $null0

#Apply CBR Traffic over UDP set cbr0 [new Application/Traffic/CBR] $cbr0 set type_ CBR $cbr0 set packetSize_ 1000 $cbr0 set rate_ 0.1Mb $cbr0 set random_ false $cbr0 attach-agent $udp0

#Schedule events $ns at 0.1 "$cbr0 start" $ns at 1.0 "$ftp0 start" $ns at 124.0 "$ftp0 stop" $ns at 124.5 "$cbr0 stop" $ns at 125.0 "Finish"

#Run Simulation $ns run

Output: #ns prog5.tcl The throughput of FTP is 72556.097560975613 bytes per second The throughput of CBR is 37363.34405144694 bytes per second

6. Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and determine the collision across different nodes. #Create Simualator set ns [new Simulator]

#Use colors to differentiate the traffics $ns color 1 Blue $ns color 2 Red #Open trace and NAM trace file set ntrace [open prog6.tr w] $ns trace-all $ntrace set namfile [open prog6.nam w] $ns namtrace-all $namfile #Finish Procedure proc Finish {} { global ns ntrace namfile #Dump all trace data and close the files $ns flush-trace close $ntrace close $namfile #Execute the NAM animation file exec nam prog6.nam & #Calculate the number of packets dropped due to collision puts "The number of packet drops due to collision is" exec grep "^d" prog6.tr | cut -d " " -f 4 | grep -c "3" & exit 0 } #Create 6 nodes for {set i 0} {$i < 6} {incr i} { set n($i) [$ns node] } #Create duplex links between the nodes $ns duplex-link $n(0) $n(2) 2Mb 10ms DropTail $ns duplex-link $n(1) $n(2) 2Mb 10ms DropTail

$ns duplex-link $n(2) $n(3) 0.2Mb 100ms DropTail #Nodes n(3), n(4) and n(5) are considered in a LAN set lan0 [$ns newLan "$n(3) $n(4) $n(5)" 0.5Mb 40ms LL Queue/DropTail MAC/802_3 Channel] #Orientation to the nodes $ns duplex-link-op $n(0) $n(2) orient right-down $ns duplex-link-op $n(1) $n(2) orient right-up $ns duplex-link-op $n(2) $n(3) orient right #Set up queue between n(2) and n(3) and monitor the queue $ns queue-limit $n(2) $n(3) 20 $ns simplex-link-op $n(2) $n(3) queuePos 0.5 #Setup TCP connection between n(0) and n(4) set tcp0 [new Agent/TCP/Newreno] $ns attach-agent $n(0) $tcp0 set sink0 [new Agent/TCPSink/DelAck] $ns attach-agent $n(4) $sink0 $ns connect $tcp0 $sink0 $tcp0 set fid_ 1 $tcp0 set window_ 8000 $tcp0 set packetSize_ 552 #Apply FTP application over TCP set ftp0 [new Application/FTP] $ftp0 attach-agent $tcp0 $ftp0 set type_ FTP #Setup another TCP connection between n(5) and n(1) set tcp1 [new Agent/TCP/Newreno] $ns attach-agent $n(5) $tcp1 set sink1 [new Agent/TCPSink/DelAck] $ns attach-agent $n(1) $sink1

$ns connect $tcp1 $sink1 $tcp1 set fid_ 2 $tcp1 set window_ 8000 $tcp1 set packetSize_ 552 #Apply FTP application over TCP set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1 $ftp1 set type_ FTP #Schedule the events $ns at 0.1 "$ftp0 start" $ns at 0.2 "$ftp1 start" $ns at 24.8 "$ftp0 stop" $ns at 24.9 "$ftp1 stop" $ns at 25.0 "Finish" #Run the simulation $ns run

7. Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and plot congestion window for different source/destination #Create Simulator set ns [new Simulator]

#Use colors to differentiate the traffics $ns color 1 Blue $ns color 2 Red

#Open trace and NAM trace file set ntrace [open prog7.tr w]

$ns trace-all $ntrace set namfile [open prog7.nam w] $ns namtrace-all $namfile

#Use some flat file to create congestion graph windows set winFile0 [open WinFile0 w] set winFile1 [open WinFile1 w]

#Finish Procedure proc Finish {} { #Dump all trace data and Close the files global ns ntrace namfile $ns flush-trace close $ntrace close $namfile

#Execute the NAM animation file exec nam prog7.nam &

Plot the Congestion Window graph using xgraph exec xgraph WinFile0 WinFile1 & exit 0 }

#Plot Window Procedure proc PlotWindow {tcpSource file} { global ns set time 0.1 set now [$ns now] set cwnd [$tcpSource set cwnd_] puts $file "$now $cwnd" $ns at [expr $now+$time] "PlotWindow $tcpSource $file" }

#Create 6 nodes for {set i 0} {$i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF