0% found this document useful (0 votes)
21 views8 pages

Fa22-Bcs-166 Assignment # 2

The document contains Tcl code for simulating a simple computer network with 4 nodes connected by links with different bandwidths and delays. TCP and UDP connections are created between nodes to transmit data using FTP and CBR applications. Event scheduling and output file handling is also demonstrated.

Uploaded by

zakirmahmood345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Fa22-Bcs-166 Assignment # 2

The document contains Tcl code for simulating a simple computer network with 4 nodes connected by links with different bandwidths and delays. TCP and UDP connections are created between nodes to transmit data using FTP and CBR applications. Event scheduling and output file handling is also demonstrated.

Uploaded by

zakirmahmood345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Fa22-bcs-166

Computer Network

Addignment # 2
Tcl File

#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open out.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


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

#===================================
# Nodes Definition
#===================================
#Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 2.0Mb 10ms DropTail
$ns queue-limit $n0 $n2 50
$ns duplex-link $n1 $n2 2.0Mb 10ms DropTail
$ns queue-limit $n1 $n2 50
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
$ns queue-limit $n2 $n3 50

#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

#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink2 [new Agent/TCPSink]
$ns attach-agent $n3 $sink2
$ns connect $tcp0 $sink2
$tcp0 set packetSize_ 1000

#Setup a UDP connection


set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set null3 [new Agent/Null]
$ns attach-agent $n3 $null3
$ns connect $udp1 $null3
$udp1 set packetSize_ 1000

#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 2.0 "$ftp0 stop"

#Setup a CBR Application over UDP connection


set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set packetSize_ 1500
$cbr1 set rate_ 1.0Mb
$cbr1 set random_ null
$ns at 1.0 "$cbr1 start"
$ns at 2.0 "$cbr1 stop"

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec awk -f Sample.awk out.tr &
exec nam out.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

Explanation

EXPLANATION OF TCL FILE CODE

set ns [new Simulator] -> here set is a command ns is a pointer variable new is command and
Simulator is inbuilt class inside ns

How to deal to deal with output files


Types of file
1) Trace file -> filename.tr
2) Nam file -> filename.nam

To create trace file


set tracefile1 [open out.tr w]-> w is write mode
$ns trace-all $tracefile1 -> trace-all traces all the events in the flow

To create nam file


set namfile1 [open out.nam w]-> w is write mode
$ns namtrace-all $namfile1

To terminate define finish procedure


1. proc finish{} {
a. global ns tracefile1 namfile1 -> global: these two can be use anywhere
b. $ns flush-race
c. close $tracefile
d. close $namfile
e. exe nam out.nam &
f. exit 0-> 0 indicates a clean exit
}

Start the simulator and End the program

$ns run -> Starts the simulation

$ns at 125.0 "finish" -> after 125 second i am ending it

How to create an inter simulator and when to start and when to stop
1. set n1[new Node] -> Node is the object or class which is available in the NS2
2. set n2[new Node]

Nodes are created now how to connect them


$ns duplex-line $n1 $n2 10Mb 10ms Drop Tail -> connecting n1 with n2 through duplex link with
bandwidth 10Mb a delay in transmission is 10ms Drop tail is used with conjunction window if the
buffer capacity of output window exceeded then the last packet arrived is lost and here we use drop
tail

Set the capacity of the Buffer


#Set queue size of the link
$ns queue-limit $n1 $n2 20 -> maximum limit of the queue is 20 it can hold maximum 20 packets

AGENTS
Link is established now on what kind of service they should work on
For connection full oriented we use TCP
For connection less service we will be using UDP
1) TCP-FTP
2) UDP-CBR

SETTING A TCP CONNECTION

1. set tcp[new Agent/TCP] -> setting a tcp pointer variable to the tcp agent
2. $ns attach-agent $n1 $tcp -> attach tcp agent to n1 n1 becomes the tcp agent (sending tcp
agent)
3. set sink [new Agent/TCPSink] destination can be initialized by using the TCPSink
4. $ns attach-agent $n2 $sink
5. $ns connect $tcp $sink -> we will connect both by using this connect keywork
6. $tcp set fid_1 -> fid is flowid it is used to differentiate between sending and receiving data
(n data flows which are happening at the same time)
7. $tcp set packetSize_552 -> if we don't send the packet size then we it will be by default 1k
(1000)
FTP
File transfer protocol is a standard mechanism provided by internet for transferring files from one
host to another
FTTP use the service of TCP
Port 21 is use for control connection and post 20 is use for data transfer

1. Set ftp [new Application/FTP]


2. $ftp attach-agent $tcp
Connectionless Service/ UDP
1. #set udp [new Agent/UDP]
2. $ns attach-agent $n1 $udp -> sender node
3. $set null [new Agent/Null]
4. $ns attach-agent $n2 $null -> receiver node
5. $ns connect $udp $null
6. $udp set fid_2

Constant Bit Rate (CBR)


 CBR is a trem used in telecommunication relating to quality of service
 CBR encoding means that the rate at which output data should be consumed is constant
 CBR is useful for streaming multimedia content

# set up cbr over udp


1. Set cbr [new Application/Traffic/CBR]
2. $cbr attach-agent $udp
3. $cbr set packetSize_1000
4. $cbr set rate_0.01Mb
5. $cbr set random_false -> it is used to control the noise over the data if set random then it
will allow some noise and block some noise and if it is false then it will not block the noise
Scheduling The Events
Event are handled by using the $ns at command
1. $ns at 0.1 ‘cbr start’ -> at point 0.1 sec start cbr
2. $ns at 1.0 ‘ftp start’
3. $ns at 124.0 “ftp stop”
4. $ns at 124.5 “cbr stop”
Network Animator( NAM )
#Giving positions to the nodes in NAM
$ns duplex-link-op $n0 $n2 orient-right-down
$ns duplex-link-op $n1 $n2 orient-right-up

AWK file
BEGIN {
recvdSize = 0
transSize = 0
startTime = 400
stopTime = 0
}
{
event = $1 # $ represent first column that is event
time = $2
send_id = $3
rec_id = $4
pkt_size = $6
flow_id = $8

# Store start time


if (send_id == "0") {
if (time < startTime) {
startTime = time
}

if (event == "+") {
# Store transmitted packet's size
transSize += pkt_size
}
}

# Update total received packets' size and store packets arrival time
if (event == "r" && rec_id == "2") {
if (time > stopTime) {
stopTime = time
}
# Store received packet's size
if (flow_id == "0") {
recvdSize += pkt_size
}
}
}

END {
# Calculate throughput
duration = startTime - stopTime
# Ensure duration is at least 1 to avoid division by zero
if (duration < 1) {
duration = 1
}
print("The duration is : ",duration)
print("The recived sized is : ",recvdSize)
throughput = (recvdSize * 8 ) / (duration) ;# Convert bits to Mbps
printf("Throughput: %.2f Mbps\n", throughput)}
Output
As command is already is in tcl file exec awk -f Sample.awk out.tr &

So run ns labtask.tcl in terminal

You might also like