Fa22-Bcs-166 Assignment # 2
Fa22-Bcs-166 Assignment # 2
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]
#===================================
# 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
#===================================
# 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
#===================================
# 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"
#===================================
# 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
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 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]
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
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
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
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 &