Sie sind auf Seite 1von 33

Page |1

J.N.T.U.H. COLLEGE OF ENGINEERING

KUKATPALLY, HYDERABAD – 500 085

CERTIFICATE
This is to certify that ______________ of B.Tech III year I Semester
bearing the Hall-Ticket number ___________ has fulfilled his/her
COMPUTER NETWORKS LAB record for the academic year 2018-2019.

Signature of the Head of the Department Signature of the staff member

Date of Examination_________________________
Internal Examiner External Examiner

JNTUHCEH
Page |2

Sno. Name of the experiment Date Page no. Sign

1. Write a C Program to show 26/06/18 3


Bit stuffing.

2. Write a C Program to show 26/07/18 5


Byte Stuffing.

3. Write a C Program to 03/07/18 7


implement CRC.

4. Write a C Program to simulate 10/07/18 10


Hamming Code method of error
correction and detection.

5. Write a C Program to simulate 17/07/18 14


Sliding Window Protocol.

6. Write a C Program to simulate 17/07/18 16


Go Back N Sliding Window
Protocol.

7. Write a C program to simulate 21/08/18 19


Selective Repeat Protocol.

8. Write a C Program to show 04/09/18 23


Shortest Path Algorithm.

9. Write a C Program to show 18/09/18 25


Distance Vector Routing
Algorithm
10. Write a C program to 18/09/18 29
implement sink tree of
broadcasting
11. Write a C Program to show 09/10/18 32
encryption

12. Write a C Program to show 09/10/18 33


decryption.

JNTUHCEH
Page |3

1. Write a C Program to show Bit stuffing.

PROGRAM:

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
int len,i,j,k,cnt=0;
char *a,*b,*c;
clrscr();
printf("Enter a string: ");
scanf("%[^\n]s",a);
len=strlen(a);
strcpy(c,"");
printf("binary:\n");
for(i=0;i<len;i++)
{
j=a[i];
itoa(j,b,2);
for(k=0;k<strlen(b);k++)
{
if(b[k]=='0')
{
cnt=0;
strcat(c,"0");
}
else
{
cnt++;
strcat(c,"1");
}
if(cnt==5)
strcat(c,"0");
}
printf("%s",b);
}
printf("\nbit stuffed:");
printf("\n%s\n",c);
cnt=0;

JNTUHCEH
Page |4

printf("removed:\n");
for(i=0;i<strlen(c);i++)
{
if(c[i]=='0')
{
cnt=0;
printf("%d",0);
}
else
{
cnt++;
printf("%d",1);
}
if(cnt==5)
{
i++;
}
}
getch();
}

OUTPUT:
Enter a string: storm
Binary:
11100111110100110111111100101101101
Bit stuffed:
1110011111001001101111101100101101101
Removed:
11100111110100110111111100101101101

JNTUHCEH
Page |5

2. Write a C Program to show Byte Stuffing.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20],finalstr[30];
char header='a',esc='\\';
int temp,i=0,j=1;
int arr[20];
clrscr();
printf("\nEnter the string : ");
gets(str);
j=0;
for(i=0;i<=strlen(str);i++)
{
if(str[i]==header||str[i]==esc)
finalstr[j++]=esc;
finalstr[j++]=str[i];
}
i=0;
printf("\n\nThe binary form of the text string is : ");
while(str[i]!=NULL)
{
temp=str[i];
j=1;
while(temp!=0)
{
arr[j]=temp%2;
temp=temp/2;
j++;
}
for(j=j-1;j!=0;j--)
{
printf("%d",arr[j]);
}
i++;
}
i=0;
printf("\n\nThe byte stuffed string is : ");
while(finalstr[i]!=NULL)
{
temp=finalstr[i];
j=1;

JNTUHCEH
Page |6

while(temp!=0)
{
arr[j]=temp%2;
temp=temp/2;
j++;
}
for(j=j-1;j!=0;j--)
{
printf("%d",arr[j]);
}
i++;
}
getch();
}

OUTPUT:

JNTUHCEH
Page |7

3. Write a C Program to implement CRC.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char*findCrc(char*frame,char*key)
{
char *crc;
int i,j,k,init;
i=0;
init=0;
do
{
if(frame[i]=='1')
{
for(j=0;j<strlen(key);j++)
{
k=frame[i]^key[j];
if(k==0)
frame[i]='0';
if(k==1)
frame[i]='1';
i++;
}
}
init++;
i=init;
}while(strlen(frame)-i>=strlen(key));
j=0;
for(i=strlen(key)-1;i>=0;i--)
{
crc[j]=frame[strlen(frame)-i];
j++;
}
crc[j]='\0';
return crc;
}
void main()
{
int i,j,ch;

JNTUHCEH
Page |8

char *bstream,*key,*frame,*crc,*zero;
clrscr();
strcpy(zero,"0");
printf("\tSender:\n");
printf("Enter Binary Stream: ");
scanf("%s",bstream);
printf("Enter Key:");
scanf("%s",key);
strcpy(frame,bstream);
for(i=0;i<strlen(key)-1;i++)
{
strcat(frame,zero);
}
strcpy(crc,findCrc(frame,key));
strcat(bstream,crc);
printf("stream to send: %s",bstream);
printf("\n\n\t\tReceiver:");
printf("\n1.Manual String Entry\n2.Take above String\nEnter choice:");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter String: ");
scanf("%s",bstream);
}
else
printf("\nString Received: %s",bstream);
printf("\nKey: %s",key);
strcpy(crc,findCrc(bstream,key));
strcpy(frame,"");
for(i=0;i<strlen(key)-1;i++)
strcat(frame,zero);
if(strcmp(frame,crc)==0)
printf("\nNo Error\n");
else
printf("\nError in string received\n");

getch();

JNTUHCEH
Page |9

OUTPUT:
Sender:
Enter binary stream: 1001011001
Enter Key: 1011
Stream to send: 1001011001100

Receiver:
1.Manual Entry
2. Take Above String
Enter choice:2
String received:10010011001100
No error

JNTUHCEH
P a g e | 10

4. Write a C Program to simulate Hamming Code method of error correction and


detection.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
int power(int a,int n)
{
int i,m=1;
for(i=0;i<n;i++)
m=m*a;
return m;
}
int findParityNum(int m)
{
//2^r>=(m+r+1)
int r=0;
while(power(2,r)<(m+r+1))
r++;
return r;
}
void tobin(int a,char*t)
{
int i;
i=0;
while(a>0)
{
t[i]=(a%2)+'0';
a=a/2;
i++;
}
t[i]='\0';
}
int toDec(char*t)
{
int k=0,i,p=1,m;
for(i=0;i<strlen(t);i++)
{
m=t[i]-'0';

JNTUHCEH
P a g e | 11

k=k+(p*m);
p=p*2;
}
return k;
}
char checkParity(char*hamcode,int pos)
{
char t[10];
int cnt,i,k;
k=log(pos)/log(2);
printf("\nP%d:",pos);
cnt=0;
for(i=pos-1;i<strlen(hamcode);i++)
{
tobin(i+1,t);
if(t[k]=='1' && hamcode[i]=='1')
{
cnt++;
}
}
if(cnt%2!=0)
{
return'1';
}
return '0';
}
void toHamming(char*bstream,int r,char*hamcode)
{
int i,k,j;
char t[10];
strrev(bstream);
j=0;
for(i=0;i<strlen(bstream)+r;i++)
{
k=log(i+1)/log(2);
if(log(i+1)/log(2)==k)
hamcode[i]='0';
else
{
hamcode[i]=bstream[j];
j++;

JNTUHCEH
P a g e | 12

}
}
hamcode[i]='\0';
for(i=0;i<r;i++)
{
hamcode[(power(2,i)-1)]=checkParity(hamcode,power(2,i));
printf("%c",hamcode[(power(2,i)-1)]);
}
strrev(hamcode);
}
void main()
{
int r,e,k,i;
char bstream[10],hamcode[20],error[10];
printf("/-----------------Generating Haming Code/Sender's End-----------------
/\n");
printf("Enter a binary stream: ");
scanf("%s",bstream);
r=findParityNum(strlen(bstream));
printf("The number of parity bits is: %d",r);
toHamming(bstream,r,hamcode);
printf("\nHamming code:%s",hamcode);
printf("\n/-----------------Correcting Haming Code/Receiver's End--------------
---/");
printf("\nGenerate error at position: ");
scanf("%d",&e);
e=strlen(hamcode)-e;
if(hamcode[e]=='1')
hamcode[e]='0';
else
hamcode[e]='1';
printf("\nThe code received is %s",hamcode);
strrev(hamcode);
for(i=0;i<r;i++)
{
error[i]=checkParity(hamcode,power(2,i));
printf("%c",error[i]);
}
k=toDec(error);
printf("\nerror at bit position:%s i.e position %d",strrev(error),k);
strrev(hamcode);

JNTUHCEH
P a g e | 13

k=strlen(hamcode)-k;
if(hamcode[k]=='1')
hamcode[k]='0';
else
hamcode[k]='1';
printf("\nCorrected code: %s",hamcode);
}

OUTPUT:
/-----------------Generating Haming Code/Sender's End-----------------/
Enter a binary stream: 1011
The number of parity bits is: 3
P1:1
P2:0
P4:0
Hamming code:1010101
/-----------------Correcting Haming Code/Receiver's End-----------------/
Generate error at position: 4

The code received is 1011101


P1:0
P2:0
P4:1
error at bit position:100 i.e position 4
Corrected code: 1010101

JNTUHCEH
P a g e | 14

5. Write a C Program to simulate Sliding Window Protocol.

PROGRAM:
#include<stdio.h>
#include<conio.h>
char data[30];
int ack[30];
int processed=0,filled=0,size=0,entered=0;
void allotwindow(char* window)
{
int i,j;
for(i=entered;i<strlen(data);i++)
{
for(j=filled;j<size;j++)
{
if(ack[i]==0)
{
window[j]=data[i];
filled++;
entered++;
printf("\nInsertion : The window consists of : ");
for(i=0;i<filled;i++)
printf("%c ",window[i]);
}
}
}
}

void acknowledge(char* window)


{
int i;
printf("\tAcknowledged %c",window[0] );
for(i=0;i<strlen(data);i++)
if(ack[i-1]==1&&window[i]==data[i])
ack[i]=1;
for(i=0;i<filled;i++)
{
window[i]=window[i+1];
}
filled--;
processed++;

JNTUHCEH
P a g e | 15

void main()
{
int i;
char window[30];
for(i=0;i<30;i++)
{
window[i]='\0';
ack[i]=0;
}
clrscr();
printf("Enter the input data : ");
gets(data);
printf("Enter the window size : ");
scanf("%d",&size);
while(processed<strlen(data))
{
allotwindow(window);
acknowledge(window);
}
getch();
}

OUTPUT:

JNTUHCEH
P a g e | 16

6. Write a C Program to simulate Go Back N Sliding Window Protocol.

PROGRAM:

#include<stdio.h>
#include<conio.h>
int f,b,buffer[20],frame[20],sent=-1,e,brk=-1;
void dequeue()
{
int i;
for(i=0;i<b-1;i++)
{
buffer[i]=buffer[i+1];
}
buffer[b-1]=-1;
}
void sendinit()
{
int i;
if(f>b)
{
for(i=0;i<b;i++)
{
buffer[i]=frame[i];
}
}
else
{
for(i=0;i<f;i++)
{
buffer[i]=frame[i];
}
}
sent=i;
}
void requesting(int request)
{
int i;
for(i=0;i<b;i++)
{
buffer[i]=frame[request+i];

JNTUHCEH
P a g e | 17

}
if(request==f)
{
brk=1;
printf("No Frames with sequence %d",f);
printf("\n\nAll frames received successfully");
}
}
void receive(int request)
{
int received;
received=buffer[0];
dequeue();
if(request==received)
{
printf("\nreceived %d",request);
}
else
{
printf("\nrequesting %d from sender",request);
requesting(request);
if(brk!=1)
receive(request);
}
}
void main()
{
int i;
printf("Enter number of frames:");
scanf("%d",&f);
printf("Enter buffer size:");
scanf("%d",&b);
for(i=0;i<f;i++)
frame[i]=i;
sendinit();
printf(“Generate Error at: ”);
scanf(“%d”,&e);
buffer[e]=-1;
i=-1;
while(1)
{

JNTUHCEH
P a g e | 18

if(brk==1)
break;
else
{
i++;
printf("\nnetwork layer requesting frame: %d",i);
receive(i);
}

}
getch();
}

OUTPUT:

Enter number of frames:6


Enter buffer size:3
Generate error at: 2

network layer requesting frame: 0


received 0
network layer requesting frame: 1
received 1
network layer requesting frame: 2
requesting 2 from sender
received 2
network layer requesting frame: 3
received 3
network layer requesting frame: 4
received 4
network layer requesting frame: 5
requesting 5 from sender
received 5
network layer requesting frame: 6
requesting 6 from sender
No Frames with sequence 6

All frames received successfully

JNTUHCEH
P a g e | 19

7. Write a C program to simulate Selective Repeat Protocol.

PROGRAM:

#include<stdio.h>
int f,b,buffer[20],frame[20],sent=-1,e,brk=-1;
void dequeue()
{
int i;
for(i=0;i<b-1;i++)
{
buffer[i]=buffer[i+1];
}
buffer[b-1]=-1;
}
void sendinit()
{
int i;
if(f>b)
{
for(i=0;i<b;i++)
{
buffer[i]=frame[i];
}
}
else
{
for(i=0;i<f;i++)
{
buffer[i]=frame[i];
}
}
sent=i;
}
void shift()
{
int i;
for(i=0;i<b;i++)
buffer[i+1]=buffer[i];

JNTUHCEH
P a g e | 20

void requesting(int request)


{
int i;
printf("\nAcknowledgement received for %d", request);
if(request>=0)
{
if(sent<f)
{
buffer[b-1]=frame[sent];
sent++;
printf("\nSending %d",sent-1);
}
}
else
{
if(request==-f)
{
brk=1;
printf("\nNo Frames with sequence %d",f);
printf("\n\nAll frames received successfully");
}
else
{
printf("\nSending %d",request*-1);
buffer[0]=frame[request*-1];
}
}
}
void receive(int request)
{
int received;
received=buffer[0];
if(request==received)
{
dequeue();
printf("\nreceived %d",request);
requesting(request);
}
else
{
requesting(request*-1);

JNTUHCEH
P a g e | 21

if(brk!=1)
receive(request);
}
}
void main()
{
int i;
printf("Enter number of frames:");
scanf("%d",&f);
printf("Enter buffer size:");
scanf("%d",&b);
for(i=0;i<f;i++)
frame[i]=i;
for(i=0;i<20;i++)
buffer[i]=-1;
sendinit();
printf("Generate Error at: ");
scanf("%d",&e);
buffer[e]=-1;
i=-1;
while(1)
{
if(brk==1)
break;
else
{
i++;
printf("\nnetwork layer requesting frame: %d",i);
receive(i);
}

}
getch();
}

OUTPUT:

Enter number of frames:6


Enter buffer size:3
Generate Error at: 2

JNTUHCEH
P a g e | 22

network layer requesting frame: 0


received 0
Acknowledgement received for 0
Sending 3
network layer requesting frame: 1
received 1
Acknowledgement received for 1
Sending 4
network layer requesting frame: 2
Acknowledgement received for -2
Sending 2
received 2
Acknowledgement received for 2
Sending 5
network layer requesting frame: 3
received 3
Acknowledgement received for 3
network layer requesting frame: 4
received 4
Acknowledgement received for 4
network layer requesting frame: 5
received 5
Acknowledgement received for 5
network layer requesting frame: 6
Acknowledgement received for -6
No Frames with sequence 6

All frames received successfully

JNTUHCEH
P a g e | 23

8. Write a C Program to show Shortest Path Algorithm.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,s,inf,cost[10][10],sh[10],v[10];
clrscr();
inf=-99;
printf("Enter number of nodes: ");
scanf("%d",&n);
printf("Enter cost matrix: order(%d x %d): \n",n,n)
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cost[i][j]);
}
}
sh[0]=0;
for(i=0;i<n;i++)
{
sh[i]=inf;
}
for(i=0;i<n;i++)
v[i]=0;
for(i=0;i<n;i++)
{
v[i]=1;
for(j=0;j<n;j++)
{
if(cost[i][j]!=0)
{
if(sh[j]==inf)
{
sh[j]=sh[i]+cost[i][j];
}
else if((sh[i]+cost[i][j])<sh[j])
sh[j]=sh[i]+cost[i][j];

JNTUHCEH
P a g e | 24

}
}
}
for(i=0;i<n;i++)
printf("%d %d",i,sh[i]);
getch();
}

OUTPUT:

Enter the number of nodes: 6


Enter the cost matrix: order(6 x 6):
044000
402361
420000
030020
060203
010030

Enter the source node: 0


0 0
1 4
2 4
3 7
4 9
5 5

JNTUHCEH
P a g e | 25

9. Write a C Program to show Distance Vector Routing Algorithm

PROGRAM:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#define I 99
void main()
{
int n,i,j,temp;
int table[15][15],dist[15],delay[15];
char node,route[15];
char nodes[15],adj[15];
clrscr();
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the nodes : ");
scanf(" %s",&nodes);
printf("\nEnter the node for which routing table is required : ");
scanf(" %c",&node);
printf("\nEnter the adjacent nodes of %c :",node);
scanf("%s",adj);
for(i=0;i<strlen(adj);i++)
{
printf("\nEnter the routing table for %c : ",adj[i]);
printf("\n");
for(j=0;j<n;j++)
{
printf("%c - ",nodes[j]);
scanf("%d",&table[i][j]);
}
}
for(j=0;j<strlen(adj);j++)
{
printf("\nEnter the delay for %c%c : ",node,adj[j]);
scanf("%d",&delay[j]);
}
for(i=0;i<strlen(nodes);i++)
{
dist[i]=I;

JNTUHCEH
P a g e | 26

for(j=0;j<strlen(adj);j++)
{
temp=delay[j]+table[j][i];
if(temp<dist[i])
{
dist[i]=temp;
route[i]=adj[j];
}
}
if(nodes[i]==node)
{
dist[i]=0;
route[i]='-';
}
}
printf("\n**NEW ESTIMATED DELAY FOR NODE %c**\n",node);
for(i=0;i<n;i++)
{
printf("\n%3d - ",dist[i]);
printf("%3c ",route[i]);
}
getch();
}

OUTPUT:

Enter the number of nodes : 12

Enter the nodes : ABCDEFGHIJKL

Enter the node for which routing table is required : J

Enter the adjacent nodes of J :AIHK

Enter the routing table for A :


A-0
B - 12
C - 25
D - 40
E - 14
F - 23

JNTUHCEH
P a g e | 27

G - 18
H - 17
I - 21
J-9
K - 24
L - 29

Enter the routing table for I :


A - 24
B - 36
C - 18
D - 27
E-7
F - 20
G - 31
H - 20
I-0
J - 11
K - 22
L - 33

Enter the routing table for H :


A - 20
B - 31
C - 19
D-8
E - 30
F - 19
G-6
H-0
I - 14
J-7
K - 22
L-9

Enter the routing table for K :


A - 21
B - 28
C - 36
D - 24
E - 22

JNTUHCEH
P a g e | 28

F - 40
G - 31
H - 19
I - 22
J - 10
K-0
L-9

Enter the delay for JA : 8

Enter the delay for JI : 10

Enter the delay for JH : 12

Enter the delay for JK : 6

**NEW ESTIMATED DELAY FOR NODE J**

8- A
20 - A
28 - I
20 - H
17 - I
30 - I
18 - H
12 - H
10 - I
0- -
6- K
15 - K

JNTUHCEH
P a g e | 29

10. Write a C program to implement sink tree of broadcasting

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define INFINITY 9999
#define MAX 20

int g[MAX][MAX],st[MAX][MAX],n,i,j;
int cost[MAX][MAX],u,v,mindist,dist[MAX];
int from[MAX],visited[MAX],edges,mincost;
int prims();

int main()
{
int i,j,tot_cost;
printf("Enter total number of nodes : ");
scanf("%d", &n);
printf("Enter adjacency matrix :\n\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("g[%d][%d] : ",i,j);
scanf("%d",&g[i][j]);
}
}
tot_cost = prims();
printf("The spanning matrix of given matrix is :\n\n");
printf("\tA\tB\tC\tD\n");
for(i=0; i<n; i++)
{
printf("%c\t", i+'A');
for(j=0; j<n; j++)
{
printf("%d\t",st[i][j]);
}
printf("\n");
}

JNTUHCEH
P a g e | 30

printf("The total cost is %d",tot_cost);


return 0;
}

int prims()
{
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(g[i][j] == 0)
cost[i][j] = INFINITY;
else
{
cost[i][j] = g[i][j];
st[i][j] = 0;
}
}
}
dist[0] = 0;
visited[0] = 1;
for(i=1; i<n; i++)
{
dist[i] = cost[0][i];
from[i] = 0;
visited[i] = 0;
}
mincost = 0;
edges = n-1;
while(edges > 0)
{
mindist = INFINITY;
for(i=1; i<n; i++)
{
if(visited[i] == 0 && dist[i] < mindist)
{
v = i;
mindist = dist[i];
u = from[v];
}
}

JNTUHCEH
P a g e | 31

st[u][v] = dist[v];
st[v][u] = dist[v];
edges--;
visited[v] = 1;
for(i=1; i<n; i++)
{
if(visited[i] == 0 && cost[i][v] < dist[i])
{
dist[i] = cost[i][v];
from[i] = v;
}
}
mincost += cost[u][v];
}
return mincost;
}

OUTPUT :

JNTUHCEH
P a g e | 32

11. Write a C Program to show encryption.

PROGRAM:
Encryption:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,datanum,temp,bincoeff,key=2;
char c,input[20],encrypt[20];
clrscr();
printf("\nEnter the input :");
gets(input);
for(i=0;i<strlen(input);i++)
{
temp=(int)input[i];
if(temp<97) bincoeff=65;
else bincoeff=97;
datanum=(((int)input[i])%bincoeff+key)%26;
encrypt[i]=(char)(datanum+bincoeff);
}
encrypt[i]='\0';
printf("\nThe encrypted data is : %s",encrypt);
getch();
}

OUTPUT:
Enter the input :Computer Networks
The encrypted data is : EqorwvgtIPgvyqtmu

JNTUHCEH
P a g e | 33

12. Write a C Program to show decryption.

PROGRAM:
Decryption:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,datanum,temp,bincoeff,key=2;
char c,input[20],encrypt[20];
clrscr();
printf("\nEnter the input :");
gets(input);
for(i=0;i<strlen(input);i++)
{
temp=(int)input[i];
if(temp<97) bincoeff=65;
else bincoeff=97;
datanum=(((int)input[i])%bincoeff-key)%26;
if(datanum<0) datanum+=26;
encrypt[i]=(char)(datanum+bincoeff);
}
encrypt[i]='\0';
printf("\nThe decrypted data is : %s",encrypt);
getch();
}

OUTPUT:
Enter the input :EqorwvgtPgvyqtmu
The decrypted data is : ComputerNetworks

JNTUHCEH

Das könnte Ihnen auch gefallen