0% found this document useful (0 votes)
62 views41 pages

CN Lab Manual

Uploaded by

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

CN Lab Manual

Uploaded by

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

L T P C

III Year – I Semester


0 0 3 1.5
COMPUTER NETWORKS LAB

Course Objectives:
Learn basic concepts of computer networking and acquire practical notions of protocols
with the emphasis on TCP/IP. A lab provides a practical approach to Ethernet/Internet
networking: networks are assembled, and experiments are made to understand the layered
architecture and how do some important protocols work

Course Outcomes:
By the end of the course student will be able to
 Know how reliable data communication is achieved through data link layer.
 Suggest appropriate routing algorithm for the network.
 Provide internet connection to the system and its installation.
 Work on various network management tools

List of Experiments:

1. Study of Network devices in detail and connect the computers in Local Area Network.
2. Write a Program to implement the data link layer farming methods such as
i) Character stuffing ii) bit stuffing.
3. Write a Program to implement data link layer farming method checksum.
4. Write a program for Hamming Code generation for error detection and correction.
5. Write a Program to implement on a data set of characters the three CRC polynomials
– CRC 12, CRC16 and CRC CCIP.
6. Write a Program to implement Sliding window protocol for Goback N.
7. Write a Program to implement Sliding window protocol for Selective repeat.
8. Write a Program to implement Stop and Wait Protocol.
9. Write a program for congestion control using leaky bucket algorithm
10. Write a Program to implement Dijkstra‘s algorithm to compute the Shortest path
through a graph.
11. Write a Program to implement Distance vector routing algorithm by obtaining
routing table at eachnode (Take an example subnet graph with weights indicating
delay between nodes).
12. Write a Program to implement Broadcast tree by taking subnet of hosts.
13. Wireshark
i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv. Analysis and Statistics & Filters.
CHARACTER STUFFING:

#include<stdio.h>
#include<string.h>
int main()
{
char a[30],fs[50]=" ",t[3],sd,ed,x[3],s[3],y[3],d[3];
int i;
printf("\n Enter a character to be stuffed: ");
scanf("%s",a);
printf("\n Enter a character that represents starting delimiter: ");
scanf(" %c",&sd);
printf("\n Enter a character that represents ending delimiter: ");
scanf(" %c",&ed);
x[0]=s[0]=s[1]=sd;
x[1]=s[2]='\0';
y[0]=d[0]=d[1]=ed;
d[2]=y[1]='\0';
strcat(fs,x);
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("\n After stuffing: %s",fs);
return 0;
}
OUTPUT:
BIT STUFFING;
#include<stdio.h>
#include<string.h>
int main()
{ char a[20],b[50]=" ",t[6],r[5];
int i,j,p=0,q=0;
printf("Enter the bit to be stuffed:");
scanf("%s",a);
strcpy(b,"01111110");
if(strlen(a)<5)
strcat(b,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(b,"111110");
i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(b,r);
}
p=0;
}
for(q=i;q<strlen(a);q++)
t[p++]=a[q];
t[p]='\0';
strcat(b,t);
}
strcat(b,"01111110");
printf("\nStuffed bits are %s",b);
strcpy(a," ");
for(i=8;i<strlen(b)-8;i++)
{
p=0;
for(j=i;j<=i+5;j++)
t[p++]=b[j];
t[p]='\0';
if(strcmp(t,"111110")==0)
{
strcat(a,"11111");
i=j-1;
}
else
{
r[0]=b[i];
r[1]='\0';
strcat(a,r);
}
}
printf("\nDestuffed bits are:%s",a);
return 0;
}

OUTPUT:
Check Sum:-
Program:
#include<stdio.h>
#include<math.h>
int main()
{
int
a,b,c,d,i,j,k,sum,csum[4],binary[8],wsum[4],cs=0,fsum,wsum1[4],binary1[8];
printf("\nEnter the 4 inputs: \n");
scanf("%d%d%d%d",&a,&b,&c,&d);
sum=a+b+c+d;
for(i=7;i>=0;i--)
{
binary[i]=sum%2;
sum=sum/2;
printf("%d",binary[i]);
}
wsum[0]=binary[0]+binary[4];
wsum[1]=binary[1]+binary[5];
wsum[2]=binary[2]+binary[6];
wsum[3]=binary[3]+binary[7];
printf("ws\n");
for(j=0;j<4;j++)
{
if(wsum[j]==0)
wsum[j]=1;
else
wsum[j]=0;
printf("%d",wsum[j]);
csum[j]=wsum[j];
}
for(k=3;k>=0;k--)
{
cs=cs+csum[k]*pow(2,k);
}
printf("\n%d\n",cs);
fsum=a+b+c+d+cs;
for(i=7;i>=0;i--)
{
binary1[i]=fsum%2;
fsum=fsum/2;
printf("%d",binary1[i]);
}
wsum1[0]=binary1[0]+binary1[4];
wsum1[1]=binary1[1]+binary1[5];
wsum1[2]=binary1[2]+binary1[6];
wsum1[3]=binary1[3]+binary1[7];
printf("\nreceiver side: \n");
for(j=0;j<4;j++)
{
if(wsum1[j]==0)
wsum1[j]=1;
wsum1[j]=0;
printf("%d",wsum1[j]);
}
if(wsum1[0]==0 && wsum1[2]==0 && wsum1[2]==0 && wsum1[3]==0)
printf("\nAccepted");
else
printf("\nNot Accepted");
return 0;
}

Output:
Hamming Code:-
Program:-
#include<stdio.h>
int main()
{
int a[10],b[10],c1,c2,c3,i,p;
printf("\n Enter the 4 digits: ");
scanf("%d%d%d%d",&a[3],&a[5],&a[6],&a[7]);
a[1]=a[3]^a[5]^a[7];
a[2]=a[3]^a[6]^a[7];
a[4]=a[5]^a[6]^a[7];
for(i=1;i<8;i++)
printf("\t%d",a[i]);
printf("\n Enter the 7 bits: ");
for(i=1;i<8;i++)
{
scanf("%d",&b[i]);
}
c1=b[1]^b[3]^b[5]^b[7];
c2=b[2]^b[3]^b[6]^b[7];
c3=b[4]^b[5]^b[6]^b[7];
p=c1*1+c2*2+c3*4;
if(p==0)
printf("\n There is no error in the data");
else
{
printf("\n There is error in the position: %d",p);
if(b[p]==0)
b[p]=1;
else
b[p]=0;
printf("\n");
for(i=1;i<8;i++)
printf("\t%d",b[i]);
}
return 0;
}
Output:-
CRC:-

#include <stdio.h>
#include <string.h>
char t[28],cs[28],g[28],N;
int a,e,c,b;
void xor()
{
for(c=1;c<N;c++)
cs[c]=((cs[c]==g[c])?'0':'1');
}
void crc()
{
for(e=0;e<N;e++)
cs[e]=t[e];
do
{
if(cs[0]=='1')
xor();
for(c=0;c<N-1;c++)
cs[c]=cs[c+1];
cs[c]=t[e++];
}while(e<=a+N-1);
}

int main()
{
int flag=0;
do{
printf("\n1.crc12 \n2.crc16 \n3.crc ccip\n4.exit\n\nEnter your option.");
scanf("%d",&b);
switch(b)
{
case 1:strcpy(g,"1100000001111");
break;
case 2:strcpy(g,"11000000000000101");
break;
case 3:strcpy(g,"10001000000100001");
break;
case 4:return 0;
}
N=strlen(g);
printf("\n enter data:");
scanf("%s",t);
printf("\n-----------------------\n");
printf("\n generating polynomial:%s",g);
a=strlen(t);
for(e=a;e<a+N-1;e++)
t[e]='0';
printf("\n--------------------------\n");
printf("mod-ified data is:%s",t);
printf("\n-----------------------\n");
crc();
printf("crc code is:%s",cs);
for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];
printf("\n-----------------------\n");
printf("\n final codeword is : %s",t);
printf("\nreceiver side--------\n");
printf("\ntest error detection 0(yes) 1(no)?:");
scanf("%d",&e);
if(e==0)
{
do{
printf("\n\tenter the position where error is to be inserted:");
scanf("%d",&e);
}
while(e==0||e>a+N-1);
t[e-1]=(t[e-1]=='0')?'1':'0';
printf("\n-----------------------\n");
printf("\n\terroneous data:%s\n",t);
}
crc();
for(e=0;(e<N-1)&&(cs[e]!='1');e++);
if(e<N-1)
printf("error detected\n\n");
else
printf("\n no error detected \n\n");
printf("\n-----------------------");

}while(flag!=1);
}
Output:-
Hamming Code:-
Program:-
#include<stdio.h>
int main()
{
int a[10],b[10],c1,c2,c3,i,p;
printf("\n Enter the 4 digits: ");
scanf("%d%d%d%d",&a[3],&a[5],&a[6],&a[7]);
a[1]=a[3]^a[5]^a[7];
a[2]=a[3]^a[6]^a[7];
a[4]=a[5]^a[6]^a[7];
for(i=1;i<8;i++)
printf("\t%d",a[i]);
printf("\n Enter the 7 bits: ");
for(i=1;i<8;i++)
{
scanf("%d",&b[i]);
}
c1=b[1]^b[3]^b[5]^b[7];
c2=b[2]^b[3]^b[6]^b[7];
c3=b[4]^b[5]^b[6]^b[7];
p=c1*1+c2*2+c3*4;
if(p==0)
printf("\n There is no error in the data");
else
{
printf("\n There is error in the position: %d",p);
if(b[p]==0)
b[p]=1;
else
b[p]=0;
printf("\n");
for(i=1;i<8;i++)
printf("\t%d",b[i]);
}
return 0;
}
Output:-
Selective Repeat and Go Back N Protocol
Program:-
#include<stdio.h>
#include<stdlib.h>
int n,r;
struct frame
{
char ack;
int data;
}frm[10];
int sender(void);
void recvack(void);
void resend_sr(void);
void resend_gb(void);
void goback(void);
void selective(void);
int main()
{
int c;
do
{
printf("\n\n 1.selective repeat ARQ \n2.goback n ARQ \n3.exit");
printf("\n enter your choice::");
scanf("%d",&c);
switch(c)
{
case 1:selective();
break;
case 2:goback();
break;
case 3:exit(0);
break;
}
}while(c<=4);
}
void goback()
{
sender();
recvack();
resend_gb();
printf("\n all frames sent successfully");
}
void selective()
{
sender();
recvack();
resend_sr();
printf("\n all frames sent successfully");
}
int sender()
{
int i;
printf("\n enter no of frames to be sent:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n enter data for frames [%d]:",i);
scanf("%d",&frm[i].data);
frm[i].ack='y';
}
return 0;
}
void recvack()
{
int i;
rand();
r=rand()%n;
frm[r].ack='n';
for(i=1;i<=n;i++)
{
if(frm[i].ack=='n')
printf("\n the frame number %d is not received",r);
}
}
void resend_sr()
{
printf("\n resending frames %d",r);
sleep(2);
frm[r].ack='y';
printf("\n the received frame id %d",frm[r].data );
}
void resend_gb()
{
int i;
printf("\n resending frame %d",r);
for(i=r;i<=n;i++)
{
sleep(2);
frm[i].ack='y';
printf("\n received data frame %d is %d",i,frm[i].data);
}
}

Output:-
Stop and wait

#include <stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int i,j ,noframes,x,x1=10,x2;
noframes=10;
i=1;
j=1;
printf("number of frames is %d",noframes);
getch();
while(noframes>0)
{
printf("\n sending frames is %d",i);
x=rand()%10;
if(x%10==0)
{
for(x2=1;x2<2;x2++)
{
printf("\n waiting for %d seconds\n",x2);
sleep(x2);
}
printf("\n sending frames %d \n",i);
x=rand()%10;
printf("\n ack for frame %d \n",j );
noframes=noframes-1;
i++;
j++;

}
printf("\n end of stop and wait control\n");
getch();
}

return 0;
}
Output:-
Leaky Bucket Algorithm:-
Program:-
#include<stdio.h>
#include<stdlib.h>
struct packet
{
int time;
int size;
}p[50];
int main()
{
int i,n,m,k=0;
int bsize,bfilled,outrate;
printf("enter the numberof packets:");
scanf("%d",&n);
printf("enter packets in the order of there arrival time\n");
for(i=0;i<n;i++)
{
printf("enter time and size:");
scanf("%d%d",&p[i].time,&p[i].size);
}
printf("enter the bucket size:");
scanf("%d",&bsize);
printf("enter the output rate:");
scanf("%d",&outrate);
m=p[n-1].time;
i=1;
k=0;
bfilled=0;
while(i<=m||bfilled!=0)
{
printf("\n\n at time %d",i);
if(p[k].time>=1)
{
if(bsize>=bfilled+p[k].size)
{
bfilled=bfilled+p[k].size;
printf("\n %d byte packet is inserted",p[k].size);
k=k+1;
}
else
{
printf("\n %d byte packet is discarded",p[k].size);
k=k+1;
}
}
if(bfilled==0)
{
printf("\n no packets to transmit");
}
else if(bfilled>=outrate)
{
bfilled = bfilled-outrate;
printf("\n %d bytes transfered",outrate);
}
else
{
printf("\n %d bytes transfered",bfilled);
bfilled=0;
}
printf("\n packets in the bucket %d byte",bfilled);
i++;
}
return 0;
}

Output:-
Dijkstra’s Algorithm to find Shortest Path for the given graph:-

Program:-

#include<stdio.h>
#include<conio.h>
#define max 15
#define infinity 999
void dij(int s);
void create_graph();
void display();
void path();
int s,n,cost[max][max],dist[max],prefix[max];

void main()
{
int i;
create_graph();
printf("\n Enter the source element: ");
scanf("%d",&s);
dij(s);
printf("\n Shortest paths: \n");
display();
path();
getch();
}

void create_graph()
{
int i,j;
printf("\n Enter the no.of nodes: ");
scanf("%d",&n);
printf("\n Enter the cost matrix: \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
}

void dij(int v)
{
int u,i,j,k,count,w,permanent[10],min,p;
for(i=1;i<=n;i++)
{
permanent[i]=0;
dist[i]=cost[v][i];
if (dist[i]!=infinity)
prefix[i]=v;
}
permanent[v]=1;
dist[v]=0;
for(i=0;i<n-1;i++)
{
min=999;
for(w=1;w<=n;w++)
if(dist[w]<min && !permanent[w])
{
min=dist[w];
u=w;
}
permanent[u]=1;
for(w=1;w<=n;w++)
if(dist[u]+cost[u][w]<dist[w] && !permanent[w])
{
dist[w]=dist[u]+cost[u][w];
prefix[w]=u;
}
}
}

void display()
{
int i;
printf("\n Vertex->Vertex via distance \n");
for(i=1;i<=n;i++)
printf("%6d->%d\tvia %d\t%d\n",s,i,prefix[i],dist[i]);
}

void path()
{
int i,j;
for(i=1;i<=n;i++)
{
printf("%d ",i);
j=prefix[i];
while(j!=0)
{
printf("<-- %d ",j);
j=prefix[j];
}
printf("\n");
}
}

Output:-
Distance Vector Routing Algorithm:-

Program:-

#include<stdio.h>
#include<conio.h>
struct node
{
int dist[20];
int from[20];
}rt[10];
void main()
{
int dmat[20][20];
int i,j,k,n,count=0;
printf("enter the no of nodes:");
scanf("%d",&n);
printf("enter the cost matrix,if no direct connection enter 99 :");
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]>rt[i].dist[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);
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\n node %d via %d distance
%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
getch();
}

Output:-
Broadcast Algorithm:-
Program:-

#include<stdio.h>
int k,cost[15][15],n,i,j,min_i,min_j,nearnodes[20],t[20][3];
int mincost=1000;
#define infinity 999
int minnode();
void main()
{
printf("\nenter the no.of nodes");
scanf("%d",&n);
printf("\n enter the cost matrix:");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
}
printf("\nenter source broadcast");
scanf("%d",&min_i);
for(i=1;i<=n;i++)
{
if(cost[min_i][i]<mincost)
{
mincost=cost[min_i][i];
min_j=i;
}
}
t[1][1]=min_i;
t[1][2]=min_j;
for(i=1;i<=n;i++)
{
if(cost[i][min_i]>cost[i][min_j])
nearnodes[i]=min_j;
else
nearnodes[i]=min_i;
}
i=1;
nearnodes[min_i]=nearnodes[min_j]=0;
for(i=2;i<n;i++)
{
j=minnode();
t[i][2]=j;
t[i][1]=nearnodes[j];
mincost=mincost+cost[j][nearnodes[j]];
nearnodes[j]=0;
for(k=1;k<=n;k++)
{
if(nearnodes[k]!=0&&cost[k][nearnodes[k]]>cost[k][j])
nearnodes[k]=j;

}
}
printf("\nmessages are sent through following routes");
for(i=1;i<n;i++)
printf("\n%d sends messages to %d\n",t[i][1],t[i][2]);
getch();
}
int minnode()
{
int m,temp,min=1000;
for(m=1;m<=n;m++)
{
if(nearnodes[m]!=0&&cost[m][nearnodes[m]]<min)
{
min=cost[m][nearnodes[m]];
temp=m;
}
}
return(temp);
}
Output:-
Wire shark:-

i. Packet Capture Using Wire shark.

Step-1:- Open Wire shark and in Capture tab click on Options.

Step-2:- Now click on Ethernet to open it.


Step-3:- It displays a file that was capturing the Packets.

ii. Starting Wire shark

Step-1:- To start Wire shark click on Start button which is located at the top
left corner of your window.
Step-2:- To stop this process click on Stop button which is just beside the Start
button.

iii. Viewing Captured Traffic.

Step-1:- In View Tab click on Packet List.


This is how it will show the list of captured packets.

Step-2:- Now click on Packet Details.


In this way all the details of the captured packets are displayed like this.

Step-3:- Now click on Packet Bytes.


It will show you the Byte Information of the captured packets.

iv. Analysis and Statistics & Filters

Step-1:- By giving any filter name we can filter out those specific packets
only.For an example, here we are given the filters DNS(Domain Name Service)
and ARP(Address Resolution Protocol).
Step-2:- In Statistics tab , click on Capture File Properties.
Now you can see the Statistical Properties of the Captured Packets.

Step-3:- Click on Product Hierarchy.


Step-4:- To see the graphical representation of the captured data, click on I/O
Graphs.
Step-5:- To analyse the captured data, in Analyze tab click on Display Filters.

You might also like