Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views20 pages

CN III Year Record

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)
19 views20 pages

CN III Year Record

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/ 20

KAKATIYA INSTITUTE OF TECHNOLOGY AND SCIENCE FOR WOMEN

COMPUTER SCIENCE AND ENGINEERING


III BTECH I SEM (R18)
COMPUTER NETWORKS LAB
INDEX

Exp Name of the Expereiment Pg.no Signature Remarks


no
1. Implement the data link layer 2
framing methods such as character,
character-stuffing and bit stuffing.
2. Write a program to compute CRC 4
code for the polynomials CRC-12,
CRC-16 and CRC CCIP
3. . Develop a simple data link layer 6
that performs the flow control using
the sliding window protocol, and
loss recovery using the Go-Back-N
mechanism.
4. Implement Dijsktra’s algorithm to 8
compute the shortest path through
a network

5. Take an example subnet of hosts 9


and obtain a broadcast tree for the
subnet.
6. Implement distance vector routing 11
algorithm for obtaining routing
tables at each node.
7. Implement data encryption and data 13
decryption
8. Write a program for congestion 15
control using Leaky bucket
algorithm
9. Write a program for frame sorting `17
technique used in buffers.
III CSE I SEM COMPUTER NETWORKS LAB
Program-01:
Implement the data link layer framing methods such as character, character-
stuffing and bit stuffing.

 Character stuffing:

Code:
#include<stdio.h>
#include<string.h>
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;
clrscr();
printf("Enter characters to be stuffed:");
scanf("%s", a);
printf("\nEnter a character that represents starting delimiter:");
scanf(" %c", &sd);
printf("\nEnter 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);
getch();
return 0;
}

2 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
Output:

 Bit Stuffing:

Code:
#include<stdio.h>
#include<string.h>
int main()
{
int a[20],b[30],i,j,k,count,n;
clrscr();
printf("Enter frame size (Example: 8):");
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}

3 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
i=k;
}}
else
{
b[j]=a[i];
}
i++;j++;
}
printf("After Bit Stuffing :");
for(i=0; i<j; i++)
printf("%d",b[i]);
getch();
return 0;
}

Output:

Program-02:
Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and
CRC CCIP

Code:
#include<stdio.h>
#include<conio.h>
int glen,dlen;
void remainder(int[],int[],int[]);
void main()
{
int data[50],ddup[50],gen[50],i,j,tlen,rem[50]={0},count=0;
clrscr();
/*accepting the data information */
printf("\n enter the length of the data to be transmitted:");
scanf("%d",&dlen);
printf("\n enter the actual data to be transmitted:");
for(i=0;i<dlen;i++)
{
scanf("%d",&data[i]);
4 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
ddup[i]=data[i];
}
/* accepting the generator information */
printf("\n enter the generator length:");
scanf("%d",&glen);
printf("\n enter the generator value:");
for(i=0;i<glen;i++)
scanf("%d",&gen[i]);
tlen=dlen+glen-1;
/*adding the (gen-1) number of zeros to data */
for(i=dlen,j=0;j<glen-1;j++,i++)
ddup[i]=0;
/* finding the remainder after stuffing (glen-1) 0's */
remainder(ddup,gen,rem);
/* now adding the last three digits of new remainder to data */
for(i=0;i<dlen;i++)
ddup[i]=data[i];
for(i=dlen,j=0;j<glen;j++,i++)
ddup[i]=rem[j+1];
/* calling remainder function for checking the correctness */
remainder(ddup,gen,rem);
for(i=0;i<glen;i++)
{
if(rem[i]!=0)
count++;
}
if(count==0)
printf("\n frame is received correctly");
else
printf("\n frame is not received correctly");
getch();
}
/*function to find the remainder value */
void remainder(int ddup[],int gen[],int rem[])
{
int n=0,i,j,a[50],k,temp;
for(i=0;i<dlen;i++)
{
temp=i;
if(ddup[i]==1)
{
for(j=0;j<glen;j++)
{
if(ddup[temp]==gen[j])
{

5 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
ddup[temp]=0;
rem[j]=0;
}
else
{
ddup[temp]=1;
rem[j]=1;
}
temp++;
}
}
}
}

Output:

Program-03:
Develop a simple data link layer that performs the flow control using the sliding
window protocol, and loss recovery using the Go-Back-N mechanism.

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char sender[50],receiver[50];
int i,winsize;
clrscr();
printf("\n ENTER THE WINDOWS SIZE : ");
scanf("%d",&winsize);
printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");

6 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
printf("\n ENTER THE DATA TO BE SENT: ");
fflush(stdin);
gets(sender);
for(i=0;i<winsize;i++)
receiver[i]=sender[i];
receiver[i]=NULL;
printf("\n MESSAGE SEND BY THE SENDER:\n");
puts(sender);
printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
for(i=0;i<winsize;i++);
printf("\n ACK:%d",i);
printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
puts(receiver);
printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
getch();
}

Output:

Program-04:
7 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
Implement Dijsktra’s algorithm to compute the shortest path through a network

Code:
#include<stdio.h>
void main()
{
int path[5][5],i,j,min,a[5][5],p,st=1,ed=5,stp,edp,t[5],index;
clrscr();
printf("enter the cost matrix\n");
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
scanf("%d",&a[i][j]);
printf("enter the paths\n");
scanf("%d",&p);
printf("enter possible paths\n");
for(i=1;i<=p;i++)
for(j=1;j<=5;j++)
scanf("%d",&path[i][j]);
for(i=1;i<=p;i++)
{
t[i]=0;
stp=st;
for(j=1;j<=5;j++)
{
edp=path[i][j+1];
t[i]=t[i]+a[stp][edp];
if(edp==ed)
break;
else
stp=edp;
}}
min=t[st];index=st;
for(i=1;i<=p;i++)
{
if(min>t[i])
{
min=t[i];
index=i;
}}
printf("minimum cost %d",min);
printf("\n minimum cost path ");
for(i=1;i<=5;i++)
{
printf("--> %d",path[index][i]);
if(path[index][i]==ed)

8 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
break;}}

Output:

Program-05:
Take an example subnet of hosts and obtain a broadcast tree for the subnet.

Code:
#include<stdio.h>
int p,q,u,v,n;
int min=99,mincost=0; int t[50][2],i,j;
int parent[50],edge[50][50];
int find(int);
void sunion(int,int);
void main()
{
clrscr();
printf("\n Enter the number of nodes"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%c\t",65+i); parent[i]=-1;
}
printf("\n");

9 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
for(i=0;i<n;i++)
{
printf("%c",65+i);
for(j=0;j<n;j++)
scanf("%d",&edge[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(edge[i][j]!=99)
if(min>edge[i][j])
{
min=edge[i][j];
u=i;
v=j;
}
p=find(u);
q=find(v);
if(p!=q)
{
t[i][0]=u;
t[i][1]=v;
mincost=mincost+edge[u][v];
sunion(p,q);
}
else
{
t[i][0]=-1;
t[i][1]=-1;
}
min=99;
}
printf("Minimum cost is %d\n Minimum spanning tree is\n" ,mincost);
for(i=0;i<n;i++)
if(t[i][0]!=-1 && t[i][1]!=-1)
{
printf("%c %c %d", 65+t[i][0], 65+t[i][1],
edge[t[i][0]][t[i][1]]);
printf("\n");
}
}
void sunion(int l,int m)
{
parent[l]=m;
}

10 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
find(int l)
{
if(parent[l]>0)
l=parent[l];
return l;
}

Output:

Program-06:
Implement distance vector routing algorithm for obtaining routing tables at each
node.

Code:
#include<stdio.h>
int p,q,u,v,n;
int min=99,mincost=0; int t[50][2],i,j;
int parent[50],edge[50][50];
int find(int);
void sunion(int,int);
void main()
{
clrscr();
printf("\n Enter the number of nodes"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%c\t",65+i); parent[i]=-1;
11 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%c",65+i);
for(j=0;j<n;j++)
scanf("%d",&edge[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(edge[i][j]!=99)
if(min>edge[i][j])
{
min=edge[i][j];
u=i;
v=j;
}
p=find(u);
q=find(v);
if(p!=q)
{
t[i][0]=u;
t[i][1]=v;
mincost=mincost+edge[u][v];
sunion(p,q);
}
else
{
t[i][0]=-1;
t[i][1]=-1;
}
min=99;
}
printf("Minimum cost is %d\n Minimum spanning tree is\n" ,mincost);
for(i=0;i<n;i++)
if(t[i][0]!=-1 && t[i][1]!=-1)
{
printf("%c %c %d", 65+t[i][0], 65+t[i][1],
edge[t[i][0]][t[i][1]]);
printf("\n");
}
}
void sunion(int l,int m)
{

12 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
parent[l]=m;
}
find(int l)
{
if(parent[l]>0)
l=parent[l];
return l;
}

Output:

Program-07:
Implement data encryption and data decryption

Code:
#include <stdio.h>
int main()
{
int i, x;
char str[100];
13 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
clrscr();
printf("\nPlease enter a string:\t");
gets(str);

printf("\nPlease choose following options:\n");


printf("1 = Encrypt the string.\n");
printf("2 = Decrypt the string.\n");
scanf("%d", &x);

//using switch case statements


switch(x)
{
case 1:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] + 3; //the key for encryption is 3 that is added to ASCII value

printf("\nEncrypted string: %s\n", str);


break;

case 2:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] - 3; //the key for encryption is 3 that is subtracted to ASCII value

printf("\nDecrypted string: %s\n", str);


break;

default:
printf("\nError\n");
}
return 0;
}

14 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
Output:

Program-08:
Write a program for congestion control using Leaky bucket algorithm.

Code:
#include<stdio.h>
#include<stdlib.h>
#define NOF_PACKETS 10

int rands(int a)
{
int rn = (rand()%10)%a;
return rn == 0 ? 1 : rn;
}

int main()
{
int packet_sz[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm=0, p_sz, p_time, op;
clrscr();
for(i = 0; i<NOF_PACKETS; ++i)
packet_sz[i] = rands(6) * 10;
for(i = 0; i<NOF_PACKETS; ++i)
printf("\npacket[%d]:%d bytes\t", i, packet_sz[i]);
printf("\nEnter the Output rate:");
scanf("%d", &o_rate);
printf("Enter the Bucket Size:");
scanf("%d", &b_size);

15 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
for(i = 0; i<NOF_PACKETS; ++i)
{
if( (packet_sz[i] + p_sz_rm) > b_size)
if(packet_sz[i] > b_size)/*compare the packet siz with bucket size*/
printf("\n\nIncoming packet size (%dbytes) is Greater than bucket capacity (%dbytes)-PACKET
REJECTED", packet_sz[i], b_size);
else
printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!");
else
{
p_sz_rm += packet_sz[i];
printf("\n\nIncoming Packet size: %d", packet_sz[i]);
printf("\nBytes remaining to Transmit: %d", p_sz_rm);
p_time = rands(4) * 10;
printf("\nTime left for transmission: %d units", p_time);
for(clk = 10; clk <= p_time; clk += 10)
{
sleep(1);
if(p_sz_rm)
{
if(p_sz_rm <= o_rate)/*packet size remaining comparing with output rate*/
op = p_sz_rm, p_sz_rm = 0;
else
op = o_rate, p_sz_rm -= o_rate;
printf("\nPacket of size %d Transmitted", op);
printf("----Bytes Remaining to Transmit: %d", p_sz_rm);
}
else
{
printf("\nTime left for transmission: %d units", p_time-clk);
printf("\nNo packets to transmit!!");
}
}
}
}
return 0;
}

16 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
Output:

Program-09:
Write a program for frame sorting technique used in buffers.

Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define FRAM_TXT_SIZ 3
#define MAX_NOF_FRAM 127
char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];
struct frame// structure maintained to hold frames
{
char text[FRAM_TXT_SIZ];
int seq_no;
}fr[MAX_NOF_FRAM],shuf_ary[MAX_NOF_FRAM];

int assign_seq_no()//function which splits message


{
int k=0,i,j;//into frames and assigns sequence no
for(i=0; i<strlen(str);k++)
17 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
{
fr[k].seq_no=k;
for(j=0;j<FRAM_TXT_SIZ&&str[i]!='\0';j++)
{
fr[k].text[j]=str[i];
i++;
}
}
printf("\nAfter assigning sequence numbers:\n");
for(i=0;i<k;i++)
{
printf("%d:",i);
for(j=0;j<3;j++)
{
printf("%c",fr[i].text[j]);
}
printf("\t");
}
return k;//k gives no of frames
}
void generate(int *random_ary, const int limit)//generate array of random nos
{int r,i=0,j;
while(i<limit)
{r=rand()%limit;
for(j=0;j<i;j++)
if(random_ary[j]==r)
break;
if(i==j)random_ary[i++]=r;
}}
void shuffle(const int no_frames)// function shuffles the frames
{
int i,j,k,*random_ary,a=no_frames;
random_ary=(int*)malloc(a*sizeof(int));
generate(random_ary,no_frames);
for(i=0;i<no_frames;i++)
shuf_ary[i]=fr[random_ary[i]];
printf("\n\nAFTER SHUFFLING:\n");
for(i=0;i<no_frames;i++)
{
printf("%d:",i);
for(j=0;j<3;j++)
{
printf("%c",shuf_ary[i].text[j]);
}
printf("\t");

18 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
}
}
void sort(const int no_frames)// sorts the frames
{
int i,j,flag=1;
struct frame hold;
for(i=0;i<no_frames-1&&flag==1;i++)// search for frames in sequence
{
flag=0;
for(j=0;j<no_frames-1-i;j++)//(based on seq no.) and display
if(shuf_ary[j].seq_no>shuf_ary[j+1].seq_no)
{
hold=shuf_ary[j];
shuf_ary[j]=shuf_ary[j+1];
shuf_ary[j+1]=hold;
flag=1;
}
}
}
int main()
{
int no_frames,i,j;
clrscr();
printf("Enter the message: ");
gets(str);
no_frames=assign_seq_no();
shuffle(no_frames);
sort(no_frames);
printf("\n\nAFTER SORTING\n");
for(i=0;i<no_frames;i++)
for(j=0;j<3;j++)
printf("%c",shuf_ary[i].text[j]);
printf("\n\n");
getch();
return 0;
}

19 KITW-CSE
III CSE I SEM COMPUTER NETWORKS LAB
Output:

20 KITW-CSE

You might also like