NS2 Code For TCP


Characteristics of TCP:

Routing in TCP:

Routing predicts how to get a packet to a particular destination. The general categories of routing are:

TCP Functions:

Basically TCP has two types of functions which are,

Connection Establishment:

Data transfer:

Connection termination:

NS2 Code For TCP:

set ns [new Simulator]


set tf [open output.tr w]

set nf [open nam.nam w]


$ns trace-all $tf

$ns namtrace-all $nf


proc finish {} {

global ns nf tf

$ns flush-trace

close $tf

close $nf

exec nam nam.nam &

exit 0



set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]


$ns duplex-link $n0 $n2 1Mb 10ms DropTail

$ns simplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n2 $n3 0.04Mb 10ms DropTail


$ns queue-limit $n0 $n2 100

$ns queue-limit $n1 $n2 100

$ns queue-limit $n2 $n3 100


$ns duplex-link-op $n0 $n2 orient right-down

$ns simplex-link-op $n1 $n2 orient right-up

$ns duplex-link-op $n2 $n3 orient right


$ns duplex-link-op $n0 $n2 queuePos 0.5

$ns simplex-link-op $n1 $n2 queuePos 0.5

$ns duplex-link-op $n2 $n3 queuePos 0.5


set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n3 $null

$ns connect $udp $null


set exp [new Application/Traffic/Exponential]

$exp attach-agent $udp

$exp set burst_time_ 0

$exp set idle_time_ 400ms

$exp set rate_ 1mb

$exp set packetSize_ 1000


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 window_ 20

$tcp set packetSize_ 555


set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp set type_ FTP


$ns at 0.2 “$exp start”

$ns at 0.3 “$ftp start”

$ns at 30000 “$exp stop”

$ns at 30000 “$ftp stop”

$ns at 30000 “finish”

$ns run

NS2 Code For TCP 


Related Pages