You are on page 1of 6

Computer Networks Lab DA 3 – TCP and UDP protcol simulation with TCL

Aryaman Kolhe
20BBS0122
Question 1

Simulate a four node point-to-point network with the links connected as follows: n0-n2, nl
n2 and n2-n3. Apply TCP agent between n0-n3 and UDP between nl-n3. Apply relevant
applications over TCP and UDP agents changing the parameter and determine the number of
packets sent by TCP/ UDP.

Code

set ns [new Simulator]


# simulator object gets instantiated

set tracefile [open out.tr w]


# namtrace - network animator. tracefile - info in file (written with awk script)

$ns trace-all $tracefile

# Opening namtrace file


set nf [open out.nam w]
# w - write mode
$ns namtrace-all $nf

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 duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail

### Transport Layer


set tcp1 [new Agent/ TCP]
$ns attach-agent $n0 $tcp1
set tcpsink1 [new Agent/ TCPSink]
$ns attach-agent $n3 $tcpsink1
$ns connect $tcp1 $tcpsink1

set udp1 [new Agent/ UDP]


$ns attach-agent $n1 $udp1
set udpsink1 [new Agent/ Null]
$ns attach-agent $n3 $udpsink1
$ns connect $udp1 $udpsink1

$ns color 1 Blue


$tcp1 set fid_ 1

### Application Layer


set ftp [new Application/ FTP]
$ftp attach-agent $tcp1

set cbr [new Application/ Traffic/ CBR]


$cbr attach-agent $udp1

# connecting upper to lower layer

proc finish {} {
# follow this spacing
global ns tracefile nf
$ns flush-trace
# flushes the previous outputs ?
close $nf
close $tracefile
exec nam out.nam &
exit 0
}

$ns at 1.0 "$ftp start"


$ns at 4.0 "$ftp stop"

$ns at 1.0 "$cbr start"


$ns at 4.0 "$cbr stop"

# Start time = 1, stop at 4 and finish procedure at 5 seconds

# ns2 is an event simulator, where events can be timed


$ns at 5.0 "finish"

$ns run
# to execute the program

Output
The simulation starts at 0 seconds. From 1 second onwards, packets are sent from node 0 to
node 3 (blue) via the TCP protocol. Simultaneously, from 1 second onwards, packets are
sent from node 1 to node 3 (black).
At 1.1 seconds – (Packets are starting to be transferred)

The blue spec in the above image denotes the acknowledgement sent back to node 0 (via
TCP).

We also see some packet loss since they are colliding at node 2.
Question 2

Simulate a four node ring network. Apply relevant applications over TCP and UDP
agents changing the parameter.

Code

# What we are doing. From 1.0 to 2.5 seconds, we are transferring packets from node 0 to
node 2 via the TCP protocol.
# From 2.6 to 4.0 seconds, we are transferring packets from node 1 to node 0 via the UDP
protocol.

set ns [new Simulator]


# simulator object gets instantiated

set tracefile [open out.tr w]


# namtrace - network animator. tracefile - info in file (written with awk script)

$ns trace-all $tracefile

# Opening namtrace file


set nf [open out.nam w]
# w - write mode
$ns namtrace-all $nf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$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 $n0 1Mb 10ms DropTail

### Transport Layer


set tcp1 [new Agent/ TCP]
$ns attach-agent $n0 $tcp1
set tcpsink1 [new Agent/ TCPSink]
$ns attach-agent $n2 $tcpsink1
$ns connect $tcp1 $tcpsink1

set udp1 [new Agent/ UDP]


$ns attach-agent $n1 $udp1
set udpsink1 [new Agent/ Null]
$ns attach-agent $n0 $udpsink1
$ns connect $udp1 $udpsink1

$ns color 1 Blue


$tcp1 set fid_ 1

### Application Layer


set ftp [new Application/ FTP]
$ftp attach-agent $tcp1

set cbr [new Application/ Traffic/ CBR]


$cbr attach-agent $udp1

# connecting upper to lower layer

proc finish {} {
# follow this spacing
global ns tracefile nf
$ns flush-trace
# flushes the previous outputs ?
close $nf
close $tracefile
exec nam out.nam &
exit 0
}

$ns at 1.0 "$ftp start"


$ns at 2.5 "$ftp stop"

$ns at 2.6 "$cbr start"


$ns at 4.0 "$cbr stop"

# ns2 is an event simulator, where events can be timed


$ns at 5.0 "finish"

$ns run
# to execute the program

Output
What we are doing. From 1.0 to 2.5 seconds, we are transferring packets from node 0 to
node 2 via the TCP protocol. From 2.6 to 4.0 seconds, we are transferring packets from node
1 to node 0 via the UDP protocol.
From node 0 to node 2 (TCP) at 1.22 seconds

From node 1 to node 0 (UDP) at 3.10 seconds

You might also like