Ex.No: Study of TCP/UDP performance using Simulation tool.
Aim:
To study the performance of TCP/UDP using NS2
Algorithm:
Step 1: Create a network simulator object ns to invoke simulation
Step 2: Using an object open a file in write mode
Step 3: Using the foresaid object write all the trace output into the file
Step 4: Using an object create a file to write all nam window output into it
Step 5: Set color to represent each flow
Step 6: Create 4 nodes
Step 7: Connect node 0 and node 2 with droptail Queue of 2 Mbps
Step 8: Connect node 1 and node 2 with droptail Queue of 2 Mbps
Step 9: Connect node 2 and node 3 with droptail Queue of 4 Mbps
Step 10: Create a TCP agent and attach to node 0 and TCP sink to node 3
Step 11: Create a UDP agent and attach to node 1 and null agent to node 3
Step 12: Set fid 1 for tcp and fid 2 udp
Step 13: Invoke ftp agent to generate traffic of 1 mbps and attach to UDP agent
Step 14: Invoke CBR agent to generate traffic of 1 mbps and attach to TCP agent
Step 15: Invoke the data transmission process for TCP and UDP at 0.1 second
Step 16: Run the simulation
Step 17: Vary the bandwidth of the link and check the how TCP and UDP behaves in NAM
window
Program:
#Create a simulator object
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the NAM trace file
set nf [open out.nam w]
set f [ open congestion.tr w ]
$ns trace-all $f
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the NAM trace file
close $nf
#Execute NAM on the trace file
exec nam out.nam &
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 duplex-link $n2 $n3 4Mb 20ms DropTail
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$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
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
$ftp set packet_size_ 1000
$ftp set rate_ 1mb
$ftp set random_ false
#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 packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 0.1 "$ftp start"
$ns at 14.0 "$ftp stop"
$ns at 14.5 "$cbr stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 15.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
#Run the simulation
$ns run
Output:
Result:
Study of TCP/UDP performance is done using NS2