You are on page 1of 9

Name: Deep Chavan

Roll No.: 20

__________________________________________________________
Date: February 6,2024

Lab Assignment No.: 03

Aim: To install and configure network simulator and learn basics of TCL
scripting.

Lab Outcome Attained: LO – 2 Demonstrate the installation and configuration of


network simulator.

Theory and Program :

TCP stands for Transmission Control Protocol. It is a transport layer protocol that
facilitates the transmission of packets from source to destination. It is a connection-
oriented protocol that means it establishes the connection prior to the communication
that occurs between the computing devices in a network. This protocol is used with an
IP protocol, so together, they are referred to as a TCP/IP.

The main functionality of the TCP is to take the data from the application layer. Then it
divides the data into a several packets, provides numbering to these packets, and finally
transmits these packets to the destination. The TCP, on the other side, will reassemble
the packets and transmits them to the application layer. As we know that TCP is a
connection-oriented protocol, so the connection will remain established until the
communication is not completed between the sender and the receiver.

Program:

#Create a simulator object set ns


[new Simulator]

#Define different colors for data flows (for NAM)


$ns color 1 Purple
$ns color 2 Red

#Open the NAM trace file set nf


[open out.nam w]
$ns namtrace-all $nf

set np [open out.tr w]


$ns trace-all $np

#Define a 'finish' procedure


proc finish {} { global ns nf np
$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]
set n4 [$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 1.7Mb 20ms DropTail
$ns duplex-link $n4 $n1 2Mb 10ms 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
$ns duplex-link-op $n1 $n4 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 $ns
attach-agent $n4 $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

#Schedule events for the CBR and FTP agents


$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n4 $tcp ; $ns detach-agent $n3 $sink"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run

Working:

set ns [new Simulator] : This line creates a simulator object named 'ns'.

$ns color 1 Purple : This line sets the color for data flows with ID 1 to Purple in the NAM
visualization tool.

$ns color 2 Red : This line sets the color for data flows with ID 2 to Red in the NAM
visualization tool.

set nf [open out.nam w] : This line opens a file named 'out.nam' for writing, which will be used
to store the trace information for the NAM visualization.

$ns namtrace-all $nf : This line enables trace capturing for all events in the simulator and
directs the output to the file 'out.nam'.
set np [open out.tr w] : This line opens a file named 'out.tr' for writing, which will be used to
store the trace information for the simulator.

$ns trace-all $np : This line enables trace capturing for all events in the simulator and directs
the output to the file 'out.tr'.

proc finish {} {...} : This block defines a procedure named 'finish'. It is called when the
simulation finishes and performs tasks such as flushing traces, closing the NAM trace file,
executing NAM on the trace file, and exiting the simulator.
set n‘m’ [$ns node] : This line creates a node object named 'n ‘m’ ' using the 'node' method of
the simulator object.

$ns duplex-link $n0 $n2 2Mb 10ms DropTail : This line creates a duplex link between
nodes n0 and n2 with a capacity of 2 Mbps, a delay of 10 ms, and a DropTail queue.

$ns duplex-link $n1 $n2 2Mb 10ms DropTail : This line creates a duplex link between nodes n1
and n2 with the same parameters as the previous line.

$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail : This line creates a duplex link between
nodes n2 and n3 with a capacity of 1.7 Mbps, a delay of 20 ms, and a DropTail queue.

$ns duplex-link $n4 $n1 2Mb 10ms DropTail : This line creates a duplex link between nodes n4
and n1 with the same parameters as before.

$ns queue-limit $n2 $n3 10 : This line sets the queue size of the link between nodes n2 and n3
to 10.

$ns duplex-link-op $n0 $n2 orient right-down : This line sets the direction of the link between
nodes n0 and n2 in the NAM visualization tool.

$ns duplex-link-op $n1 $n2 orient right-up : This line sets the direction of the link between
nodes n1 and n2 in the NAM visualization tool.

$ns duplex-link-op $n2 $n3 orient right : This line sets the direction of the link between nodes
n2 and n3 in the NAM visualization tool.

$ns duplex-link-op $n1 $n4 orient right : This line sets the direction of the link between nodes
n1 and n4 in the NAM visualization tool.

$ns duplex-link-op $n2 $n3 queuePos 0.5 : This line monitors the queue position of the link

between nodes n2 and n3 in the NAM visualization tool. set tcp [new Agent/TCP] : This line

creates a TCP agent object named 'tcp'.

$tcp set class_ 2 : This line sets the class of the TCP agent to 2. $ns attach-agent

$n4 $tcp : This line attaches the TCP agent to node n4.

set sink [new Agent/TCPSink] : This line creates a TCP sink agent object named
'sink'.
$ns attach-agent $n3 $sink : This line attaches the TCP sink agent to node n3.

$ns connect $tcp $sink : This line establishes a TCP connection between the TCP agent and
the TCP sink agent.

$tcp set fid_ 1 : This line sets the flow ID of the TCP agent to 1.

set ftp [new Application/FTP] : This line creates an FTP application object named
'ftp'.

$ftp attach-agent $tcp : This line attaches the FTP application to the TCP agent.
$ns at 1.0 "$ftp start" : This line schedules the FTP application to start at 1.0 seconds in the
simulation.

$ns at 4.0 "$ftp stop" : This line schedules the FTP application to stop at 4.0 seconds in the
simulation.

$ns at 4.5 "$ns detach-agent $n4 $tcp ; $ns detach-agent $n3 $sink" : This line detaches the
TCP and sink agents from nodes n4 and n3, respectively.

$ns at 5.0 "finish" : This line schedules the 'finish' procedure to be called after 5.0 seconds of
simulation time.

$ns run : This line starts the simulation.

Output Along With Trace:

1. For 3 nodes-
2. For 4 nodes –
Trace :

<Event> <Time> <from> <to> <pktype> <size> --- <fid> <src> <dest> <seq> <pktid>

+ 1 0 2 tcp 40 ------- 1 0.0 3.0 0 0 - 1 0 2 tcp

40 ------- 1 0.0 3.0 0 0 r 1.01016 0 2 tcp 40 ----

--- 1 0.0 3.0 0 0

+ 1.01016 2 3 tcp 40 ------- 1 0.0 3.0 0 0 -

1.01016 2 3 tcp 40 ------- 1 0.0 3.0 0 0 r

1.030348 2 3 tcp 40 ------- 1 0.0 3.0 0 0

+ 1.030348 3 2 ack 40 ------- 1 3.0 0.0 0 1 -

1.030348 3 2 ack 40 ------- 1 3.0 0.0 0 1 r

1.050536 3 2 ack 40 ------- 1 3.0 0.0 0 1

+ 1.050536 2 0 ack 40 ------- 1 3.0 0.0 0 1 -

1.050536 2 0 ack 40 ------- 1 3.0 0.0 0 1 r

1.060696 2 0 ack 40 ------- 1 3.0 0.0 0 1

+ 1.060696 0 2 tcp 1040 ------- 1 0.0 3.0 1 2

- 1.060696 0 2 tcp 1040 ------- 1 0.0 3.0 1 2

+ 1.060696 0 2 tcp 1040 ------- 1 0.0 3.0 2 3 -

1.064856 0 2 tcp 1040 ------- 1 0.0 3.0 2 3 r

1.074856 0 2 tcp 1040 ------- 1 0.0 3.0 1 2

+ 1.074856 2 3 tcp 1040 ------- 1 0.0 3.0 1 2 -

1.074856 2 3 tcp 1040 ------- 1 0.0 3.0 1 2 r

1.079016 0 2 tcp 1040 ------- 1 0.0 3.0 2 3

+ 1.079016 2 3 tcp 1040 ------- 1 0.0 3.0 2 3 -

1.079751 2 3 tcp 1040 ------- 1 0.0 3.0 2 3 r


1.099751 2 3 tcp 1040 ------- 1 0.0 3.0 1 2 +

1.099751 3 2 ack 40 ------- 1 3.0 0.0 1 4

- 1.099751 3 2 ack 40 ------- 1 3.0 0.0 1 4 r 1.104645 2 3 tcp 1040 ------- 1

0.0 3.0 2 3

+ 1.104645 3 2 ack 40 ------- 1 3.0 0.0 2 5

To examine the simulation (or, for that matter, the animation) more quantitatively, we turn to a more
detailed analysis of the trace file, which contains records for all packet events

1. r for received, d for dropped, + for enqueued, - for dequeued. Every arriving packet is enqueued,
even if it is immediately dequeued. The third packet above was the first dropped packet in the
entire simulation.
2. the time, in seconds.
3. the number of the sending node, in the order of node definition and starting at 0. If the first field
was “+”, “-” or “d”, this is the number of the node doing the enqueuing, dequeuing or dropping.
Events beginning with “-” represent this node sending the packet.
4. the number of the destination node. If the first field was “r”, this record represents the packet’s
arrival at this node.
5. the protocol.
6. the packet size, 960 bytes of data (as we requested) plus 20 of TCP header and 20 of IP header.
7. some TCP flags, here represented as “-------” because none of the flags are set. Flags include E
and N for ECN and A for reduction in the advertised winsize.
8. the flow ID. Here we have only one: flow 0. This value can be set via the fid_ variable in the Tcl
source file; an example appears in the two-sender version below. The same flow ID is used for
both directions of a TCP connection.
9. the source node (0.0), in form (node . connectionID). ConnectionID numbers here are simply an
abstraction for connection endpoints; while they superficially resemble port numbers, the node
in question need not even simulate IP, and each connection has a unique connectionID at each
end. ConnectionID numbers start at 0.
10. the destination node (2.0), again with connectionID.
11. the packet sequence number as a TCP packet, starting from 0.
12. a packet identifier uniquely identifying this packet throughout the simulation; when a packet is
forwarded on a new link it keeps its old sequence number but gets a new packet identifier.

Variations:

1. We can change the color of the data


2. We can change the orientation of the Queue
3. We can change the shape , numbers and links between the nodes
4. We can change the speed and duration of the data
5. We can change the starting and the ending of the nodes as well.

Lab outcomes: Demonstrate and measure different network scenarios and their performance behavior .
Analyze the traffic flow of different protocols

You might also like