Manual CN Lab 2015
Manual CN Lab 2015
Tataguni, Bangalore-82
SEMESTER –VI
Vision:
To be a Department of excellence at a global level in Electronics and Communication
Engineering education, incorporating Research & Innovation and Leadership training
components.
Mission:
The Department Will,
Strive to provide state of Art infrastructure in classrooms and laboratories.
Enable all-round development with individual attention and innovative teaching learning
methodology.
Impart leadership qualities into the students by exposing them to industry and research
in global Electronics and Communication Engineering domain.
th
Department of Electronics & Communication Engineering 6
Semester
3 Computer Networks Laboratory [15ECL68]
PO5: Modern Tool Usage: Create, select and apply appropriate techniques, resources
and modern engineering and IT tools including prediction and modelling to complex
engineering activities with an understanding of the limitations.
PO6: The Engineer and Society: Apply reasoning informed by contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to professional engineering practice.
PO8: Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of engineering practice.
PO9: Individual and Team Work: Function effectively as an individual, and as a member
or leader in diverse teams and in multi-disciplinary settings.
th
Department of Electronics & Communication Engineering 6
Semester
4 Computer Networks Laboratory [15ECL68]
PSO1: (Knowledge/ Skills) Explore emerging technologies in the field of Electronics &
Communication Engineering using the knowledge and skills gained.
PSO2: (Application/Analysis/Problem solving) Apply techniques in different domains to
create innovative products and services in the Communication, VLSI, DSP, and
Networking.
C0-PO-PSO MAPPING
PO PO PO PO PO PO PO PO PO PO1 PO1 PO1 PSO PSO PSO
CO 1 2 3 4 5 6 7 8 9 0 1 2 1 2 3
10ECL68.
3 3 3
1
10ECL68.
3 3 3
2
10ECL68.
3 3 3
3
LABORATORY IN-CHARGES
LABORATORY TECHNICIAN
Mr. Kiran Kumar DC, Foreman, ECE
Conduct yourself in a responsible manner at all times in the laboratory. Don’t talk
aloud or crack jokes in lab.
A lab coat should be worn during laboratory experiments.
Disconnect all the equipment and switch off the power supplies before leaving.
Observe good housekeeping practices. Replace the materials in proper place after
work to keep the lab area tidy.
Arrange the chairs before leaving the lab.
Don’ts
th
Department of Electronics & Communication Engineering 6
Semester
6 Computer Networks Laboratory [15ECL68]
SYLLABUS
COMPUTER NETWORKS LABORATORY
B.E., VI Semester, Electronics & Communication Engineering
[As per Choice Based Credit System (CBCS) scheme]
Laboratory Experiments
PART-A: Simulation experiments using NS2/ NS3/ OPNET/ NCTUNS/ NetSim/QualNet/
Packet Tracer or any other equivalent tool
th
Department of Electronics & Communication Engineering 6
Semester
7 Computer Networks Laboratory [15ECL68]
1. Implement a point to pint network with four nodes and duplex links between them.
Analyze the network performance by setting the queue size and varying the bandwidth.
2. Implement a four node point to point network with links n0-n2, n1-n2 and n2-n3. Apply
TCP agent between n0-n3 and UDP between n1-n3. Apply relevant applications over TCP
and UDP agents changing the parameter and determine the number of packets sent by
TCP/UDP.
3. Implement Ethernet LAN using n (6-10) nodes. Compare the throughput by changing
the error rate and data rate.
4. Implement Ethernet LAN using n nodes and assign multiple traffic to the nodes and
obtain congestion window for different sources/ destinations.
5. Implement ESS with transmission nodes in Wireless LAN and obtain the performance
parameters.
6. Implementation of Link state routing algorithm.
Course outcomes: On the completion of this laboratory course, the students will be
able to:
1. Design and Simulate Network elements with various protocols and standards.
2. Use the network simulator tools for learning and practice of networking algorithms.
3. Demonstrate the working of various protocols and algorithms using C programming.
th
Department of Electronics & Communication Engineering 6
Semester
8 Computer Networks Laboratory [15ECL68]
LIST OF EXPERIMENTS
Page
PART-A: Simulation experiments using NS2/ NS3/ OPNET/ NCTUNS/ No.
NetSim/QualNet/ Packet Tracer or any other equivalent tool
Implement a point to pint network with four nodes and duplex links between 20
1 them. Analyze the network performance by setting the queue size and varying the
bandwidth
Implement a four node point to point network with links n0-n2, n1-n2 and n2-n3. 23
Apply TCP agent between n0-n3 and UDP between n1-n3. Apply relevant
2
applications over TCP and UDP agents changing the parameter and determine the
number of packets sent by TCP/UDP.
th
Department of Electronics & Communication Engineering 6
Semester
9 Computer Networks Laboratory [15ECL68]
48
Write a program for a HLDC frame to perform the following.
1 i) Bit stuffing
ii) Character stuffing.
Write a program for distance vector algorithm to find suitable path for 50
2
transmission.
3 Implement Dijkstra’s algorithm to compute the shortest routing path. 53
For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify the 55
program for the cases
4
a. Without error
b. With error
5 Implementation of Stop and Wait Protocol and Sliding Window Protocol 57
Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and 62
7
determine collision across different nodes. (Extra)
Simulate the different types of internet traffic such as FTP and TELNET over a 66
8
network and analyze the throughput.(Extra)
In general, NS2 provides users with a way of specifying such network protocols
and simulating their corresponding behaviors
th
Department of Electronics & Communication Engineering 6
Semester
10 Computer Networks Laboratory [15ECL68]
th
Department of Electronics & Communication Engineering 6
Semester
11 Computer Networks Laboratory [15ECL68]
set x
You don't need to declare variables in Tcl: a variable is created automatically the first time it
is set. Tcl variables don't have types: any variable can hold any value.
To use the value of a variable in a command, use variable substitution as in the following
example:
expr $x*3
When a $ appears in a command, Tcl treats the letters and digits following it as a variable
name, and substitutes the value of the variable in place of the name. In this example, the
actual argument received by the expr command will be 32*3 (assuming that variable x was
set as in the previous example). You can use variable substitution in any word of any
command, or even multiple times within a word:
set cmd expr
set x 11
$cmd $x*$x
Command substitution:
You can also use the result of one command in an argument to another command. This is
called command substitution:
set a 44
set b [expr $a*4]
When a [ appears in a command, Tcl treats everything between it and the matching] as a
nested Tcl command. Tcl evaluates the nested command and substitutes its result into the
enclosing command in place of the bracketed text. In the example above the second
argument of the second set command will be 176.
Quotes and braces:
Double-quotes allow you to specify words that contain spaces. For example, consider
the following script:
set x 24
set y 18
set z "$x + $y is [expr $x + $y]"
After these three commands are evaluated variable z will have the value 24 + 18 is 42.
Everything between the quotes is passed to the set command as a single word. Note that (a)
command and variable substitutions are performed on the text between the quotes, and (b)
the quotes themselves are not passed to the command. If the quotes were not present, the
set command would have received 6 arguments, which would have caused an error.
Curly braces provide another way of grouping information into words. They are different from
quotes in that no substitutions are performed on the text between the curly braces:
set z {$x + $y is [expr $x + $y]}
This command sets variable z to the value "$x + $y is [expr $x + $y]".
Control structures:
th
Department of Electronics & Communication Engineering 6
Semester
12 Computer Networks Laboratory [15ECL68]
Tcl provides a complete set of control structures including commands for conditional
execution, looping, and procedures. Tcl control structures are just commands that take Tcl
scripts as arguments. The example below creates a Tcl procedure called power, which raises
a base to an integer power:
proc power {base p} {
set result 1
while {$p > 0} {
set result [expr $result * $base]
set p [expr $p - 1]
}
return $result
}
This script consists of a single command, proc. The proc command takes three arguments:
the name of a procedure, a list of argument names, and the body of the procedure, which is a
Tcl script. Note that everything between the curly brace at the end of the first line and the
curly brace on the last line is passed verbatim to proc as a single argument. The proc
command creates a new Tcl command named power that takes two arguments. You can
then invoke power with commands like the following:
power 2 6
power 1.15 5
When power is invoked, the procedure body is evaluated. While the body is executing it can
access its arguments as variables: base will hold the first argument and p will hold the
second.
The body of the power procedure contains three Tcl commands: set, while, and return. The
while command does most of the work of the procedure. It takes two arguments, an
expression ($p > 0) and a body, which is another Tcl script. The while command evaluates its
expression argument using rules similar to those of the C programming language and if the
result is true (nonzero) then it evaluates the body as a Tcl script. It repeats this process over
and over until eventually the expression evaluates to false (zero). In this case the body of the
while command multiplied the result value by base and then decrements p. When p reaches
zero the result contains the desired power of base. The return command causes the
procedure to exit with the value of variable result as the procedure's result
Where do commands come from?
As you have seen, all of the interesting features in Tcl are represented by commands.
Statements are commands, expressions are evaluated by executing commands, control
structures are commands, and procedures are commands.
Tcl commands are created in three ways. One group of commands is provided by the Tcl
interpreter itself. These commands are called builtin commands. They include all of the
commands you have seen so far and many more (see below). The builtin commands are
present in all Tcl applications.
The second group of commands is created using the Tcl extension mechanism. Tcl provides
APIs that allow you to create a new command by writing a command procedure in C or C++
that implements the command. You then register the command procedure with the Tcl
interpreter by telling Tcl the name of the command that the procedure implements. In the
future, whenever that particular name is used for a Tcl command, Tcl will call your command
th
Department of Electronics & Communication Engineering 6
Semester
13 Computer Networks Laboratory [15ECL68]
procedure to execute the command. The builtin commands are also implemented using this
same extension mechanism; their command procedures are simply part of the Tcl library.
When Tcl is used inside an application, the application incorporates its key features into Tcl
using the extension mechanism. Thus the set of available Tcl commands varies from
application to application. There are also numerous extension packages that can be
incorporated into any Tcl application. One of the best known extensions is Tk, which provides
powerful facilities for building graphical user interfaces. Other extensions provide object-
oriented programming, database access, more graphical capabilities, and a variety of other
features. One of Tcl's greatest advantages for building integration applications is the ease
with which it can be extended to incorporate new features or communicate with other
resources.
The third group of commands consists of procedures created with the proc command, such
as the power command created above. Typically, extensions are used for lower-level
functions where C programming is convenient, and procedures are used for higher-level
functions where it is easier to write in Tcl.
NS Simulator Preliminaries:
Initialization and termination aspects of the ns simulator.
Definition of network nodes, links, queues and topology.
Definition of agents and of applications.
The nam visualization tool.
Tracing and random variables.
Features of NS2:
NS2 can be employed in most Unix systems and windows. Most of the NS2 code is in C++. It
uses TCL as its scripting language, Otcl adds object orientation to TCL.NS(version 2) is an
object oriented, discrete event driven network simulator that is freely distributed and open
source.
Traffic Models: CBR, VBR, Web etc.
th
Department of Electronics & Communication Engineering 6
Semester
14 Computer Networks Laboratory [15ECL68]
Structure of NS:
NS is an object oriented discrete event simulator
Simulator maintains list of events and executes one event after another
Single thread of control: no locking or race conditions
Back end is C++ event scheduler
Protocols mostly
Fast to run, more control
Front end is OTCL
Creating scenarios, extensions to C++ protocols
fast to write and change
Platforms:
It can be employed in most Unix systems (FreeBSD, Linux, Solaris) and Windows.
Source code:
Most of NS2 code is in C++
Scripting language:
It uses TCL as its scripting language OTcl adds object orientation to TCL.
Protocols implemented in NS2:
Transport layer (Traffic Agent) – TCP, UDP
Network layer (Routing agent)
Interface queue – FIFO queue, Drop Tail queue, Priority queue
Logic link control layer – IEEE 802.2, AR
How to use NS2:
Design Simulation – Determine simulation scenario
Build ns-2 script using tcl.
Run simulation
Simulation with NS2:
Define objects of simulation.
th
Department of Electronics & Communication Engineering 6
Semester
15 Computer Networks Laboratory [15ECL68]
Variable trace
set par [open output/param.tr w]
$tcp attach $par
$tcp trace cwnd_
th
Department of Electronics & Communication Engineering 6
Semester
16 Computer Networks Laboratory [15ECL68]
Creating topology:
Two nodes connected by a link
Creating nodes
set n0 [$ns node]
set n1 [$ns node]
Creating link between nodes
$ns <link_type> $n0 $n1 <bandwidth> <delay><queue-type> $ns duplex-link$n0
$n1 1Mb 10ms DropTail
Data Sending:
Create UDP agent
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
Create CBR traffic source for feeding into UDP agent set cbr0
[new Application/Traffic/CBR]
$cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent$udp0
Create traffic sink
set null0 [new Agent/Null]
$ns attach-agent$n1 $null0
Connect two agents
$ns connect $udp0 $null0
Start and stop of data
th
Department of Electronics & Communication Engineering 6
Semester
17 Computer Networks Laboratory [15ECL68]
PROCEDURE:
STEP 1: Start
STEP 2: Create the simulator object ns for designing the given simulation
STEP 3: Open the trace file and nam file in the write mode
STEP 4: Create the nodes of the simulation using the ‘set’ command
STEP 5: Create links to the appropriate nodes using $ns duplex-link command
STEP 6: Set the orientation for the nodes in the simulation using ‘orient’ command
STEP 7: Create TCP agent for the nodes and attach these agents to the nodes
STEP 8: The traffic generator used is FTP for both node0 and node1
STEP 9: Configure node1 as the sink and attach it
STEP10: Connect node0 and node1 using ‘connect’ command
STEP 11: Setting color for the nodes
STEP 12: Schedule the events for FTP agent 10 sec
STEP 13: Schedule the simulation for 5 minutes
Event Time From To PKT PKT Flags Fid Src Dest Seq Pkt
th
Department of Electronics & Communication Engineering 6
Semester
18 Computer Networks Laboratory [15ECL68]
1. The first field is the event type. It is given by one of four possible symbols r, +, -, d
which correspond respectively to receive (at the output of the link), enqueued,
dequeued and dropped.
2. The second field gives the time at which the event occurs.
3. Gives the input node of the link at which the event occurs.
4. Gives the output node of the link at which the event occurs.
7. Some flags
8. This is the flow id (fid) of IPv6 that a user can set for each flow at the input OTcl
script one can further use this field for analysis purposes; it is also used when
specifying stream color for the NAM display.
11. This is the network layer protocol’s packet sequence number. Even though UDP
implementations in a real network do not use sequence number, ns keeps track of
UDP packet sequence number for analysis purposes
XGRAPH:
The xgraph program draws a graph on an x-display given data read from either data file or
from standard input if no files are specified. It can display upto 64 independent data sets
using different colors and line styles for each set. It annotates the graph with a title, axis
labels, grid lines or tick marks, grid labels and a legend.
Syntax:
th
Department of Electronics & Communication Engineering 6
Semester
19 Computer Networks Laboratory [15ECL68]
th
Department of Electronics & Communication Engineering 6
Semester
20 Computer Networks Laboratory [15ECL68]
PART-A
EXPERIMENTS-1
Implement a point to pint network with four nodes and duplex links between them. Analyze
the network performance by setting the queue size and varying the bandwidth.
Design:
th
Department of Electronics & Communication Engineering 6
Semester
21 Computer Networks Laboratory [15ECL68]
Note:
1. Set the queue size fixed from n0 to n2 as 10, n1-n2 to 10 and from n2-n3 as 5. Syntax:
To set the queue size
$ns set queue-limit <from> <to>
<size> Eg: $ns set queue-limit $n0 $n2
10
2. Go on varying the bandwidth from 10, 20 30. . and find the number of packets dropped
at the node 2 as shown in the Fig 1.
3. To add the agents between the nodes
i). set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
ii). set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
iii). set null [new Agent/Null]
$ns attach-agent$n3 $null
Finally, $ns connect $udp0 $null
set ns [ new Simulator]
set tf [ open lab1.tr w ]
$ns trace-all $tf
th
Department of Electronics & Communication Engineering 6
Semester
22 Computer Networks Laboratory [15ECL68]
close $nf
exit 0
}
$ns at 0.1 "$cbr0 start"
$ns at 0.1 "$cbr1 start"
$ns at 10.0 "finish"
$ns run
AWK Script:
BEGIN
{
#include<stdio.h>
count=0;
}
{
if($1=="d") #d stands for the packets drops.
count++
}
END
{
printf("The Total no of Packets Dropped due to Congestion :
%d\n\n", count)
}
Output:
ns lab1.tcl
awk –f lab1.awk lab1.tr
The Total no of packets Dropped due to congestion:4560
EXPERIMENTS-2
Implement a four node point to point network with links n0-n2, n1-n2 and n2-n3. Apply TCP
agent between n0-n3 and UDP between n1-n3. Apply relevant applications over TCP and
UDP agents changing the parameter and determine the number of packets sent by
th
Department of Electronics & Communication Engineering 6
Semester
24 Computer Networks Laboratory [15ECL68]
TCP/UDP.
Design:
Note:
1.Calculate the total number of packets sent by TCP and UDP by changing the bandwidth
from 100Mb,25Mb or 200mb in the Topology and the data rate from 0.1, 0.001 as shown
below.
Eg:
Bandwidth Data Rate TCP pkt sent UDP pkt sent
100Mb 0.1 2000 2500
200Mb 0.001 3400 6700
#The below code is used to set the color and labels to the #links.
th
Department of Electronics & Communication Engineering 6
Semester
26 Computer Networks Laboratory [15ECL68]
#The below code is used to set the packet size of ftp and #udp.
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.001
#The below code is used to increase the data rate (if the #interval is more than the
more number of packets goes to #destination).
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.001
#This code is used give a color red->tcp and blue ->udp.
$tcp0 set class_ 1
$udp1 set class_ 2
# The below code is used connect the agents. $ns connect $tcp0
$sink3 $ns connect $udp1 $null3
proc finish { } {
global ns nf tf
$ns flush-trace
close $tf
exit 0
}
$ns at 0.1 "$cbr1 start"
$ns at 0.2 "$ftp0 start"
$ns at 5.0 "finish"
$ns run
th
Department of Electronics & Communication Engineering 6
Semester
27 Computer Networks Laboratory [15ECL68]
AWK Script:
BEGIN{
#include<stdio.h>
tcp=0;
udp=0;
}
{
if($1=="r"&&$3=="2"&&$4=="3"&& $5=="tcp")
tcp++;
if($1=="r"&&$3=="2"&&$4=="3"&&$5=="cbr")
udp++;
}
END{
printf("\n Total number of packets sent by TCP : %d\n",tcp);
printf("\n Total number of packets sent by UDP : %d\n",udp);
}
Output:
ns lab2.tcl
awk –f lab2.awk lab2.tr
Total number of packets sent by TCP: 1200
Total number of packets sent by UDP: 4500
EXPERIMENTS-3
th
Department of Electronics & Communication Engineering 6
Semester
28 Computer Networks Laboratory [15ECL68]
Implement an Ethernet LAN using n nodes (6-10), change the error rate and data rate and
compare the throughput.
Design:
Note:
1. The lan can be created by using the command:
$ns make-lan “ $n0 $n1 $n2 $n3 ” 100Mb 10ms LL Queue/DropTail Mac/802_
2. The Error between the nodes n3 and n6 can be added as follows:
Set err [ new ErrorModel ]
$ns lossmodel $err $n3 $n6
$err set rate_ 0.1 # used to set error rate.
3. The throughput can analyzed by changing the datarate and errorrate as shown
below.
First, Fix the data rate to 0.0001 and vary the error rate then throughput decreases as shown
below.
Now Fix the error rate to 0.1 and vary the data rate then throughput increases as shown
below.
Program:
$ns make-lan "$n0 $n1 $n2 $n3" 10Mb 10ms LL Queue/DropTail Mac/802_3
# The below code is used to add an error model between the #nodes n3 and n6.
# This is the error rate. Change this ;#rate to add errors between n3 and n6.
proc finish { } {
global nf ns tf
exec nam lab5.nam &
close $nf
close $tf
exit 0
}
$ns at 5.0 "finish"
$ns at 0.1 "$cbr1 start"
$ns run
AWK Script:
th
Department of Electronics & Communication Engineering 6
Semester
31 Computer Networks Laboratory [15ECL68]
BEGIN
{
#include <stdio.h>
pkt=0;
time=0
}
{
if($1="r" && $3=="8" && $4=="5")
{
pkt=pkt+$6
time=$2
}
}
END
{
printf(" Throughput: %fMbps\n\n",(pkt/time)*(8/1000000));
}
Output:
ns lab5.tcl
awk –f lab5.awk lab5.tr
Throughput:148Mbps
EXPERIMENTS-4
Implement Ethernet LAN using n nodes and assign multiple traffic to the nodes and obtain
th
Department of Electronics & Communication Engineering 6
Semester
32 Computer Networks Laboratory [15ECL68]
Step 2: Attach these files (file1.tr & file2.tr) to the agents tcp0 and tcp1 as
shown below.
Step 3: To trace the congestion window value we use the trace command to trace
Then, the congestion window values are stored in the file1 and file2.
th
Department of Electronics & Communication Engineering 6
Semester
33 Computer Networks Laboratory [15ECL68]
ns lab7.tcl
awk –f lab7.awk file1.tr > tcp1
awk –f lab7.awk file2.tr > tcp2
xgraph –x “Time” -y “cwnd” tcp1 tcp2
Program:
set ns [new Simulator]
set tf [open lab7.tr w]
$ns trace-all $tf
set nf [open lab7.nam w]
$ns namtrace-all $nf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$ns make-lan "$n0 $n1 $n2 $n3" 10mb 10ms LL Queue/DropTail Mac/802_3
}
{
if($6=="cwnd_")
printf("%f \t %f \n", $1,$7);
}
END
{
puts "DONE"
}
Output:
ns lab7.tcl
awk –f lab7.awk file1.tr>tcp1
awk –f lab7.awk file2.tr>tcp2
xgraph –x “time” –y “convalue ” tcp1 tcp2
Note: To set the foreground and background color of the graph choose the appropriate
options by giving xgraph –
EXPERIMENTS-5
Implement ESS with transmission nodes in Wireless LAN and obtain the performance
parameters.
th
Department of Electronics & Communication Engineering 6
Semester
36 Computer Networks Laboratory [15ECL68]
Design:
Simulation Parameters:
Program:
set ns [new Simulator]
set tf [open lab8.tr w]
th
Department of Electronics & Communication Engineering 6
Semester
37 Computer Networks Laboratory [15ECL68]
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \ -propType
Propagation/TwoRayGround \ -antType
Antenna/OmniAntenna \ -topoInstance $topo \
-agentTrace ON \
-routerTrace ON
create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 label "tcp0"
$n1 label "sink1/tcp1"
$n2 label "sink2"
#The below code is used to give the initial node positions. $n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
$n1 set X_ 100
$n1 set Y_ 100
th
Department of Electronics & Communication Engineering 6
Semester
38 Computer Networks Laboratory [15ECL68]
$n1 set Z_ 0
$n2 set X_ 600
$n2 set Y_ 600
$n2 set Z_ 0
$ns at 0.1 "$n0 setdest 50 50 15"
$ns at 0.1 "$n1 setdest 100 100 25"
$ns at 0.1 "$n2 setdest 600 600 25"
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1
$ns connect $tcp0 $sink1
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp1 $sink2
$ns at 5 "$ftp0 start"
$ns at 5 "$ftp1 start"
#The below code is used to provide the node movements.
proc finish {} {
global ns nf tf
$ns flush-trace
exec nam lab8.nam &
th
Department of Electronics & Communication Engineering 6
Semester
39 Computer Networks Laboratory [15ECL68]
close $tf
exit 0
}
$ns at 250 "finish"
$ns run
AWK Script:
BEGIN
{
#include<stdio.h>
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1=="r"&&$3=="_1_"&&$4=="AGT")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1=="r"&&$3=="_2_"&&$4=="AGT")
{
count2++
pack2=pack2+$8
time2=$2
}
}
th
Department of Electronics & Communication Engineering 6
Semester
40 Computer Networks Laboratory [15ECL68]
END
{
printf("The Throughput from n0 to n1: %fMbps\n",
((count1*pack1*8)/(time1*1000000)))
Output:
ns lab8.tcl
awk –f lab8.awk lab8.tr
The Throughput from n0 to n1: 5444Mbps
The Throughput from n1 to n2: 345Mbps
EXPERIMENTS-6
Implementation of Link state routing algorithm.
Design:
th
Department of Electronics & Communication Engineering 6
Semester
41 Computer Networks Laboratory [15ECL68]
Program
set ns [new Simulator]
proc finish {} {
global nf ns tr
$ns flush-trace
close $tr
exec nam out.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
th
Department of Electronics & Communication Engineering 6
Semester
42 Computer Networks Laboratory [15ECL68]
$ns rtproto LS
$ns run
th
Department of Electronics & Communication Engineering 6
Semester
43 Computer Networks Laboratory [15ECL68]
PART-B
EXPERIMENTS-1
Write a program for
i) Bit stuffing ii) Character stuffing.
i) Bit stuffing
Theory:
th
Department of Electronics & Communication Engineering 6
Semester
44 Computer Networks Laboratory [15ECL68]
Security and Error detection are the most prominent features that are to be provided by any
application which transfers data from one end to the other end. One of such a mechanism in
tracking errors which may add up to the original data during transfer is known as Stuffing. It
is of two types namely Bit Stuffing and the other Character Stuffing. Coming to the Bit
Stuffing, 01111110 is appended within the original data while transfer of it. The following
program describes how it is stuffed at the sender end and de-stuffed at the receiver end.
Program:
#include
main()
{
int a[15];
int i,j,k,n,c=0,pos=0;
clrscr();
printf("\n Enter the number of bits");
scanf("%d",&n);
printf("\n Enter the bits");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==1)
{
c++;
if(c==5)
{
pos=i+1;
c=0;
for(j=n;j>=pos;j--)
{
k=j+1;
a[k]=a[j];
}
a[pos]=0;
n=n+1;
}
}
else
c=0;
}
th
Department of Electronics & Communication Engineering 6
Semester
45 Computer Networks Laboratory [15ECL68]
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf(" 01111110 ");
getch();
}
Output:
Theory:
Coming to the Character Stuffing, DLESTX and DLEETX are used to denote start and end of
character data with some constraints imposed on repetition of charaters as shown in the
program below clearly.
th
Department of Electronics & Communication Engineering 6
Semester
46 Computer Networks Laboratory [15ECL68]
Program:
#include
#include
#include
void charc(void);
void main()
{
int choice;
while(1)
{
printf("\n\n\n1.character stuffing");
printf("\n\n2.exit");
printf("\n\n\nenter choice");
scanf("%d",&choice);
printf("%d",choice);
if(choice>2)
printf("\n\n invalid option....please renter");
switch(choice)
{
case 1:
charc();
break;
case 2:
exit(0);
}
}
}
void charc(void)
{
char c[50],d[50],t[50];
int i,m,j;
clrscr();
printf("enter the number of characters\n");
scanf("%d",&m);
printf("\n enter the characters\n");
for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
th
Department of Electronics & Communication Engineering 6
Semester
47 Computer Networks Laboratory [15ECL68]
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='d';
j++;
d[j]='l';
j++;
d[j]='e';
j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;
d[m]='d';
m++;
d[m]='l';
m++;
d[m]='e';
m++;
d[m]='e';
m++;
d[m]='t';
m++;
d[m]='x';
m++;
printf("\n\n transmitted data: \n");
for(i=0;i<m;i++)
{
printf("%c",d[i]);
}
for(i=6,j=0;i<m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e')
i=i+3;
t[j]=d[i];
}
printf("\n\nreceived data:");
for(i=0;i<j;i++)
th
Department of Electronics & Communication Engineering 6
Semester
48 Computer Networks Laboratory [15ECL68]
{printf("%c",t[i]);
}
}
Output:
EXPERIMENTS-2
Write a program for distance vector algorithm to find suitable path for transmission
Theory:
Distance Vector routing (DVR) algorithm is unlike Dijkstra's algorithm which is a non-adaptive
routing algorithm and means that it is purely static, that is pre-destined and fixed, not flexible
in networks where congestions are more prone to occur. DVR is an adaptive routing
th
Department of Electronics & Communication Engineering 6
Semester
49 Computer Networks Laboratory [15ECL68]
algorithm in which the information from neighbours is maitained well by each and every node
and this helps us to determine the simplest path possible in a changing network. Though, one
of the node may fail, still, the destined node is reachable through other possible intermediate
nodes that are found out by the DVR algorithm. The perfectly executing program below
shows it live below.
Program:
#include
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
th
Department of Electronics & Communication Engineering 6
Semester
50 Computer Networks Laboratory [15ECL68]
for(i=0;i<n;i++)
{
printf("\n\nState value for router %d is \n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
}
EXPERIMENTS-3
Implement Dijkstra’s algorithm to compute the shortest routing path.
Theory:
Dijkstra's algorithm is a non-adaptive routing algorithm which is very widely used to route
packets from source to detination through various routers available during the transmission.
It is implemented at the network layer of the architecture where data packets are sent
th
Department of Electronics & Communication Engineering 6
Semester
51 Computer Networks Laboratory [15ECL68]
through routers which maitain routing tables that help to denote the exact location to where
the destined packets need to be delivered. Major advantage in using Dijkstra's algorithm is
that it forwards the data packets from source to destination through the most optimized path
in terms of both the distance and cost observed. It prompts the user to enter the number of
nodes and the source and destination nodes among them. In addition, the algorithm written
below also asks for the neighbours to each node with the distances to reach to them from
each node is also prompted. All this data is stored and used further to calculate and estimate
the best path possible for data packets to reach their destination from source. Program
below explains it in a much better way.
Program:
#include
#include
int n,s,nb,nbs[15],snbs[15],delay[15][15],i,j,temp[15],ze=0;
void min();
void main()
{
clrscr();
printf("Enter the no.of nodes:");
scanf("%d",&n);
printf("\nEnter the source node:");
scanf("%d",&s);
printf("\nEnter the no.of Neighbours to %d:",s);
scanf("%d",&nb);
printf("\nEnter the Neighbours:");
for(i=1;i<=nb;i++)
scanf("%d",&nbs[i]);
printf("\nEnter the timedelay form source to nbs:");
for(i=1;i<=nb;i++)
scanf("%d",&snbs[i]);
for(i=1;i<=nb;i++)
{
printf("\nEnter the timedelay of %d: ",nbs[i]);
for(j=1;j<=n;j++)
scanf("%d",&delay[i][j]);
}
for(i=1;i<=nb;i++)
{
printf("\nThe timedelays of %d: ",nbs[i]);
for(j=1;j<=n;j++)
printf("%3d",delay[i][j]);
}
th
Department of Electronics & Communication Engineering 6
Semester
52 Computer Networks Laboratory [15ECL68]
min();
getch();
}
void min()
{
int sum,k,y=1,store=1;
printf("\n\t\t\tnew- rout");
printf("\n\t\t\ttime-");
printf("\n\t\t\tdelay");
printf("\n");
for(i=1;i<=n;i++)
{
sum=0;
k=1;
for(j=1;j<=nb;j++)
{
temp[k++]=delay[j][i];
}
sum=temp[1]+snbs[1];
for(y=2;y<=nb;y++)
{
if(sum>temp[y]+snbs[y])
{
sum=temp[y]+snbs[y];
store=y;
}
}
if(s==i)
printf("\n\t%d+\t%d =\t%d --",ze,ze,ze);
else
printf("\n\t%d +\t%d =\t%d\t%d",temp[store],snbs[store],sum,nbs[store]);
}
}
Output:
th
Department of Electronics & Communication Engineering 6
Semester
53 Computer Networks Laboratory [15ECL68]
EXPERIMENTS-4
For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify the program for
th
Department of Electronics & Communication Engineering 6
Semester
54 Computer Networks Laboratory [15ECL68]
the cases
a. Without error
b. With error
Theory:
CRC means Cyclic Redundancy Check. It is the most famous and traditionally successful
mechanism used in error detection through the parity bits installed within the data and
obtaining checksum which acts as the verifier to check whether the data retrieved at the
receiver end is genuine or not. Various operations are involved in implementing CRC on a
data set through CRC generating polynomials. In the program, I have also provided the user to
opt for Error detection whereby he can proceed for it. Understand the program below as it is
much simpler than pretended to be so.
Program:
#include
#include
#include
#define N strlen(g)
void xor() {
for(c=1;c}
void crc() {
for(e=0;e do {
if(cs[0]=='1') xor();
for(c=0;c cs[c]=t[e++];
}while(e<=a+N-1);
}
void main() {
clrscr();
printf("\nEnter poly : "); scanf("%s",t);
printf("\nGenerating Polynomial is : %s",g);
a=strlen(t);
for(e=a;e
printf("\nModified t[u] is : %s",t);
crc();
printf("\nChecksum is : %s",cs);
for(e=a;e printf("\nFinal Codeword is : %s",t);
printf("\nTest Error detection 0(yes) 1(no) ? : ");
th
Department of Electronics & Communication Engineering 6
Semester
55 Computer Networks Laboratory [15ECL68]
scanf("%d",&e);
if(e==0) {
printf("Enter position where error is to inserted : ");
scanf("%d",&e);
t[e]=(t[e]=='0')?'1':'0';
printf("Errorneous data : %s\n",t);
}
crc();
for (e=0;(e<n-1)&&(cs[e]!='1');e++);
if(e
else printf("No Error Detected.");
getch();
}
Output:
EXPERIMENTS-5
th
Department of Electronics & Communication Engineering 6
Semester
56 Computer Networks Laboratory [15ECL68]
include<stdio.h>
int timer=0,wait_for_ack=-1,frameQ=0,cansend=1,t=0;
main()
{
int i,j,k;
int frame[5];
//clrscr();
printf("enter the time when data frame will be ready\n");
for(j=0;j<3;j++)
{
sender( i, &frame);
recv(i);
}
}
{
wait_for_ack++;
if(wait_for_ack==3)
{
}
if(i==frame[t])
{
frameQ++;
t++;
}
if(frameQ==0)
printf("NO FRAME TO SEND at time=%d \n",i);
th
Department of Electronics & Communication Engineering 6
Semester
57 Computer Networks Laboratory [15ECL68]
}
else
printf("
WAITING FOR FRAME AT TIME %d\n",i);
// return 0;
d",&frames[i]);
printf("\nWith sliding window protocol the
frames will be sent in the following manner
(assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage
sender waits for acknowledgement sent by
the receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames
sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
th
Department of Electronics & Communication Engineering 6
Semester
59 Computer Networks Laboratory [15ECL68]
if(f%w!=0)
printf("\nAcknowledgement of above frames
sent is received by sender\n");
return 0;
}
th
Department of Electronics & Communication Engineering 6
Semester
60 Computer Networks Laboratory [15ECL68]
EXPERIMENTS-6
Write a program for congestion control using leaky bucket algorithm
• Also, when the bucket is full, any additional water that enters into the bucket spills over the
sides and is lost.
• The same concept can be applied to packets in the network. Consider that data is coming
from the source at variable speeds. Suppose that a source sends data at 12 Mbps for 4
seconds. Then there is no data for 3 seconds. The source again transmits data at a rate of 10
Mbps for 2 seconds. Thus, in a time span of 9 seconds, 68 Mb data has been transmitted.
th
Department of Electronics & Communication Engineering 6
Semester
61 Computer Networks Laboratory [15ECL68]
If a leaky bucket algorithm is used, the data flow will be 8 Mbps for 9 seconds. Thus constant
flow is maintained.
Algorithm:
1. Start
2. Set the bucket size or the buffer size.
3. Set the output rate.
4. Transmit the packets such that there is no overflow.
5. Repeat the process of transmission until all packets are transmitted. (Reject packets
where its size is greater than the bucket size)
6. Stop
Code
#include<stdio.h>
#include<stdlib.h>
#define MIN(x,y) (x>y)?y:x
int main()
{
int orate,drop=0,cap,x,count=0,inp[10]={0},i=0,nsec,ch;
printf("\n enter bucket size : ");
scanf("%d",&cap);
printf("\n enter output rate :");
scanf("%d",&orate);
do
{
printf("\n enter number of packets coming at second %d :",i+1);
scanf("%d",&inp[i]);
i++;
printf("\n enter 1 to contiue or 0 to quit..........");
scanf("%d",&ch);
}
while(ch);
nsec=i;
printf("\n second \t recieved \t sent \t dropped \t remained \n");
for(i=0;count || i<nsec;i++)
{
printf(" %d",i+1);
printf(" \t%d\t ",inp[i]);
printf(" \t %d\t ",MIN((inp[i]+count),orate));
if((x=inp[i]+count-orate)>0)
{
if(x>cap)
{
th
Department of Electronics & Communication Engineering 6
Semester
62 Computer Networks Laboratory [15ECL68]
count=cap;
drop=x-cap;
}
else
{
count=x;
drop=0;
}
}
else
{
drop=0;
count=0;
}
printf(" \t %d \t %d \n",drop,count);
}
return 0;
}
th
Department of Electronics & Communication Engineering 6
Semester
63 Computer Networks Laboratory [15ECL68]
EXTRA EXPERIMENTS-1
Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and determine
collision across different nodes.
Design:
Note:
1. The collision of packets takes place at the nodes n0, n1 and n2. The “c” in the trace file
indicating the collision has been occurred at the nodes.
2. The command -trace on is used to see the collision “c” in the trace file as shown below.
$ns make-lan -trace on “$n0 $n1 $n2 $n3” 10Mb 10ms LL
Queue/DropTail Mac/802_3
3. The number of collisions occurred at different nodes are obtained by running the awk
script.
Program:
set ns [new Simulator]
th
Department of Electronics & Communication Engineering 6
Semester
64 Computer Networks Laboratory [15ECL68]
AWK Script:
BEGIN{
#include<stdio.h>
count=0
}
{
if($1=="c") {
printf("The [ %s ] Packet occured collision at the
node:[ %s ]\n",$5,$3);
th
Department of Electronics & Communication Engineering 6
Semester
66 Computer Networks Laboratory [15ECL68]
count++
}
}
END{
printf("\n***************************************\n");
printf("\nThe Total Packet Collision %d", count);
printf("\n***************************************\n");
}
EXTRA EXPERIMENTS-2
th
Department of Electronics & Communication Engineering 6
Semester
67 Computer Networks Laboratory [15ECL68]
Simulate the different types of internet traffic such as FTP and TELNET over a
network and analyze the throughput.
Design:
Note:
1. Change the data rate or the bandwidth of either tcp or the telnet and analyze the
throughput
Eg: To vary the data rate of telnet or tcp we use:
$telnet1 set interval_ 0.01 & $ftp0 set interval_ 0.0001
To vary the packet size of telnet or ftp we use
$telnet1 set packetSize_ 5000 & $ftp0 set paceketSize_ 2000
Program:
set ns [new Simulator]
set tf [open lab3.tr w]
$ns trace-all $tf
set nf [open lab3.nam w]
$ns namtrace-all $nf
set n0 [$ns node]
th
Department of Electronics & Communication Engineering 6
Semester
68 Computer Networks Laboratory [15ECL68]
AWK Script:
BEGIN{
#include<stdio.h>
ftppack=0
telpack=0
ftptime=0
teltime=0
}
{
if($1=="r"&&$3=="2"&&$4=="3")
{
ftppack=ftppack+$6;
ftptime=$2;
}
if($1=="r"&&$3=="4"&&$4=="5")
{
telpack=telpack+$6;
teltime=$2;
}
}
END{
printf("Throughput of Ftp:%fMbps”,(ftppack/ftptime)*(8/1000000));
printf("Throughput of tel:%fMbps”,(telpack/teltime)*(8/1000000));
}
Output:
ns lab3.tcl
awk –f lab3.tcl lab3.tr
Throughput of Ftp:67 Mbps
Throughput of tel:72 Mbps
th
Department of Electronics & Communication Engineering 6
Semester
70 Computer Networks Laboratory [15ECL68]
VIVA QUESTIONS
1. Define Network?
2. What is a Link?
3. What is a node?
4. What is a gateway or Router?
5. What is point-point link?
6. What is Multiple Access?
7. What are the advantages of Distributed Processing?
8. What are the criteria necessary for an effective and efficient network?
9. Name the factors that affect the performance of the network?
10. Name the factors that affect the reliability of the network?
11. Name the factors that affect the security of the network?
12. What are the key elements of protocols?
13. What are the key design issues of a computer Network?
14. Define Bandwidth and Latency?
15. Define Routing?
16. What is a peer-peer process?
17. When a switch is said to be congested?
18. What is semantic gap?
19. What is Round Trip Time?
20. Define the terms Unicasting, Multicasting and Broadcasting?
21. What is Multiplexing?
22. List the layers of OSI
23. Which layers are network support layers?
24. Which layers are user support layers?
25. Which layer links the network support layers and user support layers?
26. What are the concerns of the Physical Layer?
27. What are the responsibilities of Data Link Layer?
28. What are the responsibilities of Network Layer?
29. What are the responsibilities of Session Layer?
th
Department of Electronics & Communication Engineering 6
Semester
71 Computer Networks Laboratory [15ECL68]
th
Department of Electronics & Communication Engineering 6
Semester
72 Computer Networks Laboratory [15ECL68]
th
Department of Electronics & Communication Engineering 6
Semester