Professional Documents
Culture Documents
USN :___________________________
VTU Syllabus
COMPUTER NETWORKS LABORATORY
(As per the CBCS scheme)
Laboratory Experiments
Part A
1. Implement a point – to – point network with four nodes and duplex links between them.
Analyze the network performance by setting the queue size and vary the bandwidth and
find the number of packets dropped.
2. Implement a four node point-to-point network with the links connected as follows: 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 nodes (6-10), throughput by compare the throughput
by changing the error rate and data rate.
4. Implement Ethernet LAN using n nodes and assign multiple traffic nodes and plot
congestion window for different source / destination.
5. Implement ESS with transmitting nodes in wire-less LAN and obtain the performance
parameters
6. Implementation of Link state routing algorithm
Part B
Implement the following using C
1. Write a program for a HDLC frame to perform the following i) Bit stuffing ii) Character
stuffing
2. Write a program for distance vector algorithm to find suitable path for transmission.
3. Implement Dijkstra’s algorithm to compute the shortest routing path
4. For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify the program
for the cases a. Without error b. With error
5. Implementation of stop and wait protocol and sliding window protocol
6. Write a program for congestion control using Leaky bucket algorithm
TABLE OF CONTENTS
Implement Ethernet LAN using n nodes and assign multiple traffic nodes and plot
4 congestion window for different source / destination.
Implement ESS with transmitting nodes in wire-less LAN and obtain the
5 performance parameters
Course objectives:
This course will enable students to:
Choose suitable tools to model a network and understand the protocols at various OSI
reference levels.
Design a suitable network and simulate using a Network simulator tool.
Simulate the networking concepts and protocols using C/C++ programming.
Model the networks for different configurations and analyze the results.
Course outcomes:
On the completion of this laboratory course, the students will be able to:
Choose suitable tool to model a network and understand the protocols at various OSI
reference levels.
Design a suitable network and simulate using a Network simulator tool.
Simulate the networking concepts and protocols using C/C++.
Model the networks for different configuration and analyze the results.
Part A
Introduction to NS2:
Widely known as NS2, is simply an event driven simulation tool.
Useful in studying the dynamic nature of communication networks.
Simulation of wired as well as wireless network functions and protocols (e.g., routing
algorithms, TCP, UDP) can be done using NS2.
In general, NS2 provides users with a way of specifying such network protocols and
simulating their corresponding behaviors.
Tcl scripting
• Tcl is a general purpose scripting language. [Interpreter]
• Tcl runs on most of the platforms such as Unix, Windows, and Mac.
• The strength of Tcl is its simplicity.
• It is not necessary to declare a data type for variable prior to the usage
Which is thus the first line in the tcl script? This line declares a new variable as using the set
command, you can call this variable as you wish, In general people declares it as ns because
it is an instance of the Simulator class, so an object the code[new Simulator] is indeed the
installation of the class Simulator using the reserved word new.
In order to have output files with data on the simulation (trace files) or files used for
visualization (nam files), we need to create the files using “open” command:
#Open the Trace file set tracefile1 [open out.tr w]
The above commands creates a dta trace file called “out.tr” and a nam visualization trace file
called “out.nam”.Within the tcl script, these files are not called explicitly by their names,but
instead by pointers that are declared above and called “tracefile1” and “namfile” respectively.
Remark that they begins with a # symbol. The second line open the file “out.tr” to be used for
writing, declared with the letter “w”. The third line uses a simulator method called trace-all
that have as parameter the name of the file where the traces will go.
The last line tells the simulator to record all simulation traces in NAM input format. It also
gives the file name that the trace will be written to later by the command $ns flush-trace.In
our case,this will be the file pointed at by the pointer “$namfile”,i.e the file “out.tr”.
The termination of the program is done using a “finish” procedure.
#Define a ‘finish’ procedure
Proc finish { } {
$ns flush-trace
Close $tracefile1
Close $namfile
E0
}
The word proc declares a procedure in this case called finish and without arguments. The
word global is used to tell that we are using variables declared outside the procedure. The
simulator method “flush-trace” will dump the traces on the respective files. The tcl
command “close” closes the trace files defined before and exec executes the nam program for
visualization. The command exit will ends the application and return the number 0 as status
to the system. Zero is the default for a clean exit. Other values can be used to say that is a exit
because something fails.
At the end of ns program we should call the procedure “finish” and specify at what time the
termination should occur. For example,
$ns at 125.0 “finish”
It will be used to call “finish” at time 125sec.Indeed,the at method of the simulator allows us
to schedule events explicitly.
The simulation can then begin using the command
$ns run
Once we define several nodes, we can define the links that connect them. An example of a
definition of a link is:
$ns duplex-link $n0 $n2 10Mb 10ms DropTail
Which means that $n0 and $n2 are connected using a bi-directional link that has 10ms of
propagation delay and a capacity of 10Mb per sec for each direction.
To define a directional link instead of a bi-directional one, we should replace “duplex-link”
by “simplex-link”.
In NS, an output queue of a node is implemented as a part of each link whose input is that
node. The definition of the link then includes the way to handle overflow at that queue. In our
case, if the buffer capacity of the output queue is exceeded then the last packet to arrive is
dropped. Many alternative options exist, such as the RED (Random Early Discard)
mechanism, the FQ (Fair Queuing), the DRR (Deficit Round Robin), the stochastic Fair
Queuing (SFQ) and the CBQ (which including a priority and a round-robin scheduler).
In ns, an output queue of a node is implemented as a part of each link whose input is that
node. We should also define the buffer capacity of the queue related to each link. An example
would be:
The command $ns attach-agent $n0 $tcp defines the source node of the tcp connection.
The command
set sink [new Agent /TCPSink]
Defines the behavior of the destination node of TCP and assigns to it a pointer called sink.
TCP has many parameters with initial fixed defaults values that can be changed if mentioned
explicitly. For example, the default TCP packet size has a size of 1000bytes.This can be
changed to another value, say 552bytes, using the command $tcp set packetSize_ 552.
When we have several flows, we may wish to distinguish them so that we can identify them
with different colors in the visualization part. This is done by the command $tcp set fid_ 1
that assigns to the TCP connection a flow identification of “1”.We shall later give the flow
identification of “2” to the UDP connection.
A UDP source and destination is defined in a similar way as in the case of TCP.
Instead of defining the rate in the command $cbr set rate_ 0.01Mb, one can define the time
interval between transmission of packets using the command.
Scheduling Events
NS is a discrete event based simulation. The tcp script defines when event should occur. The
initializing command set ns [new Simulator] creates an event scheduler, and events are then
scheduled using the format:
The scheduler is started when running ns that is through the command $ns run.
The beginning and end of the FTP and CBR application can be done through the following
command
TCP has many parameters with initial fixed defaults values that can be changed if mentioned
explicitly. For example, the default TCP packet size has a size of 1000bytes.This can be
changed to another value, say 552bytes, using the command $tcp set packetSize_ 552.
When we have several flows, we may wish to distinguish them so that we can identify them
with different colors in the visualization part. This is done by the command $tcp set fid_ 1
that assigns to the TCP connection a flow identification of “1”.We shall later give the flow
identification of “2” to the UDP connection.
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.
11. Network layer protocol's packet sequence number. Note that even though UDP
implementations do not use sequence number, NS keeps track of UDP packet
sequence number for analysis purposes.
12. Unique id of the packet
Experiment 1
Aim: Implement a point – to – point network with duplex links between them. Analyze the
network performance by setting the queue size and vary the bandwidth and find the number
of packets dropped.
s1.awk
BEGIN{
count = 0;
}
{
event = $1;
if(event == "d") { count++; }
}
END{
printf("\nNumber of packets dropped is: %d\n", count);
}
s1.tcl
#create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#create labels
$n0 label "TCPSource"
$n3 label "TCPSink"
#set color
$ns color 1 red
#create traffic: FTP: create FTP source agent on top of TCP and attach to TCP agent
set ftp [new Application/FTP]
$ftp attach-agent $tcp
#schedule events
$ns at 0.2 "$ftp start"
$ns at 2.5 "$ftp stop"
$ns at 3.0 "finish"
$ns run
Steps
1) In the command line type gedit s1.awk
2) gedit s1.tcl
3) to run sudo ns s1.tcl
4) Vary the bandwidth from node 0 to 2 and keeping same bandwidth between 2 to 3 and
note down packets dropped.
5) Gedit s1graph and type the above result as below
s1graph
0.25 0
0.50 0
0.75 0
1.00 0
1.25 6
1.50 8
2.00 9
3.00 11
4.00 12
5.00 10
Experiment 2:
2. Implement a four node point-to-point network with the links connected as follows: 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.
s2.awk
BEGIN {
ctcp = 0;
cudp = 0;
}
{
pkt = $5;
if(pkt == "cbr") { cudp++; }
if(pkt == "tcp") { ctcp++; }
}
END {
printf("\nNo of packets sent\nTcp : %d \n Udp : %d\n", ctcp, cudp);
}
s2.tcl
proc finish {} {
Experiment 3
Implement Ethernet LAN using n nodes (6-10), change error rate and data rate and
compare throughput.
s3.awk
BEGIN {
sSize = 0;
startTime = 5.0;
stopTime = 0.1;
Tput = 0;
}
{
event = $1;
time = $2;
size = $6;
if(event == "+")
{
if(time < startTime)
{
startTime = time;
}
}
if(event == "r")
{
if(time > stopTime)
{
stopTime = time;
}
sSize += size;
}
Tput = (sSize / (stopTime-startTime))*(8/1000);
printf("%f\t%.2f\n", time, Tput);
}
END {
}
s3.tcl
proc finish {} {
global ns namfile tracefile
$ns flush-trace
close $namfile
close $tracefile
exec nam s3.nam &
Output:
#gedit s3.tcl
#sudo ns s3.tcl
1. Timestamp
2. Source node id of the flow
3. Source port id
4. Destination node id of the flow
5. Destination port id
6. Name of the variable being traced (cwnd_ , t_seqno_ , throughput, reverse_feedback)
7. Value of the traced variable
Experiment 4
Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.
s4.awk
BEGIN {
}
{
if($6 == "cwnd_")
{
printf("%f\t%f\n", $1, $7);
}
}
END {
}
s4.tcl
proc finish {} {
global ns namfile tracefile
$ns flush-trace
close $namfile
close $tracefile
$ns make-lan "$n3 $n4 $n5 $n6 $n7 $n8" 512Kb 40ms LL Queue/DropTail Mac/802_3
Output
#gedit s4.tcl
#sudo ns s4.tcl
Implement Ethernet LAN using n nodes and assign multiple traffic nodes and plot congestion
window for different source / destination.
5. Simulate simple ESS and with transmitting nodes in wire-less LAN by simulation and
determine the performance with respect to transmission of packets.
s5.awk
BEGIN{
PacketRcvd = 0;
Throughput = 0.0;
}
{
if(($1 == "r")&&($3 == "_3_")&&($4 == "AGT")&&($7 == "tcp")&&($8 > 1000))
{
PacketRcvd++;
}
}
END {
Throughput = ((PacketRcvd*1000*8) / (95.0*1000000));
printf("\nThe throughput is:%f\n", Throughput);
}
s6.tcl
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam s5.nam &
exec awk -f s5.awk s5.tr &
exit 0
}
#label node
$n(1) label "TCPSource"
$n(3) label "Sink"
Output
#gedit s5.tcl
#sudo ns s5.tcl 5
Experiment 6
proc finish {} {
global ns namfile tracefile
$ns flush-trace
close $namfile
close $tracefile
exec nam s6.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
$ns rtproto LS
$ns at 45 "finish"
$ns run
Output
#gedit s6.tcl
#sudo ns s6.tcl
Part B
BIT STUFFING
#include<string.h>
#include<stdio.h>
main()
{
char a[20],fs[50]="",t[6],r[5];
int i,j,p=0,q=0;
if(strlen(a)<5)
{
strcat(fs,a);
}
else
{
for(i=0;i<strlen(a)-4;i++)
{
for(j=i;j<i+5;j++)
{
t[p++]=a[j];
}
t[p]='\0';
if(strcmp(t,"11111")==0)
{
strcat(fs,"111110");
i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(fs,r);
}
p=0;
}
for(q=i;q<strlen(a);q++)
{
t[p++]=a[q];
}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
getch();
}
main()
{
char a[30],fs[50]="",t[3],sd,ed,x[3],s[3],d[3],y[3];
int i,j,p=0,q=0;
for(i=0;i<strlen(a);i++)
{
t[0]=a[i];
t[1]='\0';
if(t[0]==sd)
strcat(fs,s);
else
if(t[0]==ed)
strcat(fs,d);
else
strcat(fs,t);
}
strcat(fs,y);
printf("\nAfter stuffing : %s",fs);
getch();
}
2. Write a program for distance vector algorithm to find suitable path for
transmission.
#include<stdio.h>
int n, e, s[20][20], graph[20][20];
void initialize()
{
int i, j;
for (i = 1; i <= 20; i++)
for (j = 1; j <= 20 && (s[i][j] = graph[i][j] = 0); j++);
}
void getgraph()
{
int i, strt, end;
printf("Enter no. of routers & edges in the graph: ");
scanf("%d%d", &n, &e);
while (e-- > 0)
{
printf("Enter start router -> end router : ");
scanf("%d%d", &strt, &end);
graph[strt][end] = graph[end][strt] = 1;
}
}
void gettable(int src)
{
int i, j;
printf("Enter information for Source router %d.\n", src);
for (i = 1; i <= n; i++)
if (graph[src][i] == 1)
{
printf("Enter distance from source router %d to %d :", src, i);
scanf("%d", &s[src][i]);
}
printf("Enter the contents of Echo packetof Adacentj routers of %d\n",src);
for (i = 1; i <= n; i++)
if (graph[src][i] == 1)
{
printf("Enter the contents of Echo packet of router %d.\n", i);
for (j = 1; j <= n; j++)
{
if (i == j) continue;
printf("Enter distance from router %d to %d :", i, j);
scanf("%d", &s[i][j]);
}
}
}
void process(int src, int dest)
{
int min = 999, i, delay, via;
for (i = 1; i <= n; i++)
if (graph[src][i] == 1)
{
delay = s[src][i] + s[i][dest];
if (delay < min)
{
min = delay;
via = i;
}
}
printf("Suitable path from router %d to %d is through router %d with delay %d units\n", src,
dest, via, min);
}
int main()
{
int src, dest;
initialize();
getgraph();
printf("Enter the Source & Destination router\n");
scanf("%d%d", &src, &dest);
gettable(src);
process(src, dest);
return 0;
}
#include<stdio.h>
void sort(void);
void main()
{
int i,j,k,l,m,n=0,point;
char initial,dest,path[10]={' '};
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
scanf("%d",&dsp[i][j]);
}
fflush(stdin);
printf("\n enter the source node:");
scanf("%c",&initial);fflush(stdin);
printf("\n Enter the destination node:");
scanf("%c",&dest);
permanent[perm].src=initial;
permanent[perm].dest=initial;
permanent[perm++].length=0;
i=permanent[perm-1].dest-97;
for(j=0;j<nodes;j++)
{
if(i!=j)
{
if(dsp[i][j]>0)
{
temp[tem].src=permanent[perm-1].src;
temp[tem].dest=j+97;
temp[tem++].length=dsp[i][j];
}
}
}
sort();
while(tem>=0)
{
j=permanent[perm-1].dest-97;
for(i=0;i<nodes;i++)
{
if(i!=initial-97)
{
if(dsp[j][i]>0)
{
l=-1;
for(k=0;k<perm;k++)
{
if(permanent[k].dest==(i+97))
l=k;
}
for(k=0;k<=tem;k++)
{
if(temp[k].dest==(i+97))
l=k;
}
if(l<0)
{
temp[tem].src=j+97;
temp[tem].dest=i+97;
for(m=0;m<perm;m++)
{
if(permanent[m].dest==temp[tem].src)
n=permanent[m].length;
}
temp[tem++].length=dsp[j][i]+n;
}
else
{
for(m=0;m<perm;m++)
{
if(permanent[m].dest==j+97)
{
n=permanent[m].length+dsp[j][i];break;
}
else
n=dsp[j][i];
}
if((n<temp[l].length))
{
temp[l].length=n;
temp[l].src=j+97;
temp[l].dest=i+97;
}
}
}
}
}
sort();
}
printf("\nShortest path:\n");
printf("From %c to %c is:",initial,dest);
for(i=0;i<perm-1;i++)
{
if(permanent[i].dest==dest)
{
point=i;n=i; break;
}
} i=0;
for(j=perm;j>0;j--)
{
if(permanent[j-1].dest==permanent[point].src)
{
path[i++]=permanent[point].dest;
point=j-1;
}
}
path[i]=initial;
for(j=i;j>=0;j--)
printf("%c ",path[j]);
printf("\t length=%d",permanent[n].length);
getch();
}
void sort()
{
int i,j,k;
for(i=0;i<=tem;i++)
{
k=1;
for(j=0;j<=tem;j++)
{
if((temp[j].length <= temp[j+1].length))
{
stemp=temp[j];
temp[j]=temp[j+1];
temp[j+1]=stemp; k=0;
}
}
if(k)
break;
}
4. For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify the
program for the cases
a. Without error
b. With error
Algorithm
1. Given a bit string, append 0S to the end of it (the number of 0s is the same as the degree of the
generator polynomial) let B(x) be the polynomial corresponding to B.
2. Divide B(x) by some agreed on polynomial G(x) (generator polynomial) and determine the
remainder R(x). This division is to be done using Modulo 2 Division.
5. Let T’ represent the bit stream the receiver gets and T’(x) the associated polynomial.
The receiver divides T1(x) by G(x). If there is a 0 remainder, the receiver concludes T = T’ and
no error occurred otherwise, the receiver concludes an error occurred and requires a
retransmission.
#include<stdio.h>
int a[100],b[100],i,j,len,k,count=0;
//Generator Polynomial:g(x)=x^16+x^12+x^5+1
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,};
int main()
{
void div();
printf("\nEnter the length of Data Frame :");
scanf("%d",&len);
printf("\nEnter the Message :");
for(i=0;i<len;i++)
scanf("%d",&a[i]);
printf("%2d",b[i]);
div();
for(i=0;i<len;i++)
if(a[i]!=0)
{
printf("\nERROR in Recived Data");
return 0;
}
printf("\nData Recived is ERROR FREE");
}
void div()
{
for(i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(j=i;j<17+i;j++)
a[j]=a[j]^gp[count++];
}
count=0;
}
}
// rand();
noframes=10;
i=1;
j=1;
//noframes=noframes/8;
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
for(i=1;i<=f;i++)
scanf("%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]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}
Theory
The congesting control algorithms are basically divided into two groups: open loop and closed
loop. Open loop solutions attempt to solve the problem by good design, in essence, to make sure
it does not occur in the first place. Once the system is up and running, midcourse corrections are
not made. Open loop algorithms are further divided into ones that act at source versus ones that
act at the destination. In contrast, closed loop solutions are based on the concept of a feedback
loop if there is any congestion. Closed loop algorithms are also divided into two sub categories:
explicit feedback and implicit feedback. In explicit feedback algorithms, packets are sent back
from the point of congestion to warn the source. In implicit algorithm, the source deduces the
existence of congestion by making local observation, such as the time needed for
acknowledgment to come back.
The presence of congestion means that the load is (temporarily) greater than the resources (in part
of the system) can handle. For subnets that use virtual circuits internally, these methods can be
used at the network layer. Another open loop method to help manage congestion is forcing the
packet to be transmitted at a more predictable rate. This approach to congestion management is
widely used in ATM networks and is called traffic shaping.
The other method is the leaky bucket algorithm. Each host is connected to the network by an
interface containing a leaky bucket, that is, a finite internal queue. If a packet arrives at the queue
when it is full, the packet is discarded. In other words, if one or more process are already queued,
the new packet is unceremoniously discarded. This arrangement can be built into the hardware
interface or simulate d by the host operating system. In fact it is nothing other than a single server
queuing system with constant service time.
The host is allowed to put one packet per clock tick onto the network. This mechanism turns an
uneven flow of packet from the user process inside the host into an even flow of packet onto the
network, smoothing out bursts and greatly reducing the chances of congestion.
LEAKY BUCKET
#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)
{
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;
}
Viva Questions
1. Explain the functions of OSI layers ?
2. Differentiate between TCP/IP Layers and OSI Layers
3. Why header is required?
4. What is the use of adding header and trailer to frames?
5. What is encapsulation?
6. Why fragmentation requires?
7. What is MTU?
8. Which layer imposes MTU?
9. Differentiate between flow control and congestion control.
10. Differentiate between Point-to-Point Connection and End-to-End connections.
11. What are protocols running in different layers?
12. What is Protocol Stack?
13. Differentiate between TCP and UDP.
14. Differentiate between Connectionless and connection oriented connection.
15. Why frame sorting is required?
16. What is meant by subnet?
17. What is meant by Gateway?
18. What is an IP address?
19. What is MAC address?
20. Why IP address is required when we have MAC address?
21. What is meant by port?
22. What are ephemerical port number and well known port numbers?
23. What is a socket?
24. What are the parameters of socket()?
25. Describe bind(), listen(), accept(),connect(), send() and recv().
26. What are system calls? Mention few of them.