character Stuffing:-: Department of CSE Computer Networks Lab Sheet No
character Stuffing:-: Department of CSE Computer Networks Lab Sheet No
Regd.No:
Sheet
No:
-:Character Stuffing:#include<stdio.h> #include<string.h> void main() { int i=0,j=0,n,pos; char a[20],b[50],ch; clrscr(); printf("\nEnter the string:"); scanf("%s",&a); n=strlen(a); printf("\nEnter position:"); scanf("%d",&pos); while(pos>n) { printf("Invalid position,Enter again:"); scanf("%d",&pos); } printf("\nEnter the Character:"); ch=getche(); b[0]='d'; b[1]='l'; b[2]='e'; b[3]='s'; b[4]='t'; b[5]='x'; j=6; while(i<n) { if(i==pos-1) { b[j]='d'; b[j+1]='l'; b[j+2]='e'; b[j+3]=ch; b[j+4]='d'; b[j+5]='l'; b[j+6]='e'; j=j+7; } if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e') { b[j]='d'; b[j+1]='l'; b[j+2]='e'; j=j+3; }
Sheet
No:
b[j]=a[i]; i++; j++; } b[j]='d'; b[j+1]='l'; b[j+2]='e'; b[j+3]='e'; b[j+4]='t'; b[j+5]='x'; b[j+6]='\0'; printf("\nFrame after stuffing : "); printf("%s",b); getch(); }
-:Output:Enter the string:per Enter position:3 Enter the Character:e Frame after stuffing : dlestxpedleedlerdleetx
Sheet
No:
#include<stdio.h> void main() { int a[20],b[30],i,j,k,count,n; clrscr(); printf("\nEnter frame length : "); scanf("%d",&n); printf("Enter input frame (0's&1's only) : "); 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; count=0; } i=k; } } else b[j]=a[i]; i++; j++; } printf("\nFrame after stuffing : "); for(i=0;i<j;i++) printf(" %d",b[i]); getch(); }
-:Output:Enter frame length : 8 Enter input frame (0's&1's only) : 1 1 1 1 1 1 0 1 Frame after stuffing : 1 1 1 1 1 0 1 0 1
Sheet
No:
/* Cyclic Redundancy Check */ #include<stdio.h> int gen[4],genlen,frlen,rem[4]; void main() { int i,j,fr[8],dupfr[11],recvfr[11],totlen,flag; clrscr(); frlen=8; genlen=4; printf("\nEnter frame : "); for(i=0;i<frlen;i++) { scanf("%d",&fr[i]); dupfr[i]=fr[i]; } printf("\nEnter Generator : "); for(i=0;i<genlen;i++) { scanf("%d",&gen[i]); } totlen=frlen+genlen-1; for(i=frlen;i<totlen;i++) { dupfr[i]=0; } remainder(dupfr); for(i=0;i<frlen;i++) { recvfr[i]=fr[i]; } for(i=frlen;i<totlen;i++) { recvfr[i]=dupfr[i]; } remainder(recvfr); flag=0; for(i=0;i<4;i++) { if(rem[i]!=0) flag++; } if(flag==0) printf("\nFrame Received Correctly"); else printf("\nThe Received Frame is Wrong"); getch(); }
remainder(int fr[]) {
Srinivasa Institute of Engineering & Technology Cheyyeru
Sheet
No:
int k,k1,i,j; for(k=0;k<frlen;k++) { if(fr[k]==1) { k1=k; for(i=0,j=k;i<genlen;i++,j++) { rem[i]=fr[j]^gen[i]; for(i=0;i<genlen;i++) { fr[k1]=rem[i]; k1++; } } } }
Sheet
No:
#include <stdio.h> #include <math.h> void main() { int d,e,i,n,p,q,et,ip,pt,ciph; clrscr(); printf("Enter Number : "); scanf("%d",&ip); printf("\nEnter prime No's p,q : "); scanf("%d%d",&p,&q); n=p*q; et=(p-1)*(q-1); for(i=2;i<et;i++) { if(gcd(et,i)==1) printf("\te=%d",i); } printf("\n\nSelect e value : "); scanf("%d",&d); for(i=et/d;((d*i)%et)==1;i++); { e=++i; } printf("\nEntered Text : %d\nEncryption Key : %d\nDeycryption Key : %d",ip,d,e); ciph=En_De(e,ip,n); pt=En_De(d,ciph,n); printf("\nEncrypted Text(CipherText) : %d",ciph); printf("\nDecrypted Text(PlainText) : %d",pt); getch(); } int gcd(int a,int b) { int c; while(1) { c=a%b; if(c==0) return b; a=b,b=c; } }
Sheet
No:
-:Output:Enter Number : 88 Enter prime No's p,q : 11 e=3 e=7 e=21 e=23 e=27 e=41 e=43 e=47 e=61 e=63 e=67 e=81 e=83 e=87 e=101 e=103 e=107 e=121 e=123 e=127 e=141 e=143 e=147 Select e value : 7 Entered Text : 88 Encryption Key : 7 Deycryption Key : 23 Encrypted Text(CipherText) : 11 Decrypted Text(PlainText) : 88 17 e=9 e=11 e=13 e=17 e=19 e=29 e=31 e=33 e=37 e=39 e=49 e=51 e=53 e=57 e=59 e=69 e=71 e=73 e=77 e=79 e=89 e=91 e=93 e=97 e=99 e=109 e=111 e=113 e=117 e=119 e=129 e=131 e=133 e=137 e=139 e=149 e=151 e=153 e=157 e=159
Sheet
No:
/* Shortest Path Routing Algorithm*/ #include<stdio.h> void main() { int path[6][6],i,j,min,a[6][6],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 number of Paths : "); 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("\nMinimum Cost : %d",min); printf("\n\nMinimum Cost Path ");
Sheet
No:
if(path[index][i]==ed) break;
} getch(); }
-:Output:Enter 0 1 1 0 3 2 1 0 1 4 Enter Enter 1 2 1 4 1 2 1 3 the Cost Matrix : 3 5 2 1 2 3 0 1 1 3 0 5 3 0 0 number of Paths : 4 Possible Paths : 3 5 0 5 0 0 3 4 5 5 0 0
Sheet
No:
#include<stdio.h> int graph[12][12]; void main() { int i,j,no,hop; clrscr(); printf("Enter No.of Nodes : "); scanf("%d",&no); printf("\nEnter the Adjacency Matrix\n__________________________\n\n"); for(i=0;i<no;i++) { for(j=0;j<no;j++) { printf("Enter values for %d,%d Position : ",(i+1),(j+1)); scanf("%d",&graph[i][j]); } } for(i=0;i<no;i++) { for(j=0;j<no;j++) { if(graph[i][j]!=0&&j>i) printf("\nhop-%d sends a Packet to hop-%d",i,j); } } getch(); }
Sheet
No:
__________________________ Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter hop-0 hop-0 hop-1 hop-2 values values values values values values values values values values values values values values values values sends sends sends sends for for for for for for for for for for for for for for for for a a a a 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 3,1 3,2 3,3 3,4 4,1 4,2 4,3 4,4 Position Position Position Position Position Position Position Position Position Position Position Position Position Position Position Position to to to to : : : : : : : : : : : : : : : : 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
hop1
1
hop0
4
hop3
3
hop2
-:Distance Vector Routing Algorithm:/* Distance Vector Routing Algorithm */ #include<stdio.h> #include<ctype.h> int graph[12][12];
Srinivasa Institute of Engineering & Technology Cheyyeru
Sheet
No:
int e[12][12]; int ad[12]; int no,id,adc,small,chosen; char nodes[12]={'a','b','c','d','e','f','g','h','i','j','k','l'}; int main() { int i,j,k,ch1,ch2=0; adc=0; clrscr(); printf("Enter No.of Nodes : "); scanf("%d",&no); printf("\nEnter the Adjacency Matrix\n__________________________\n\n"); for(i=0;i<no;i++) for(j=0;j<no;j++) { printf("Enter values for %d,%d Position : ",(i+1),(j+1)); scanf("%d",&graph[i][j]); } printf("\nEnter Initial Estimates\n_______________________\n"); for(i=0;i<no;i++) { printf("\nEstimate for Node-%c \n",nodes[i]); for(j=0;j<no;j++) { printf("to Node-%c : ",nodes[j]); scanf("%d",&e[i][j]); } } do { printf("\n\t-:Menu:-\t\n_________________________"); printf("\n1.Routing Information for Node"); printf("\n2.Estimated Table\n\n"); printf("Select any one from the above Options : "); scanf("%d",&ch1); switch(ch1) { case 1: printf("\nWhich Node should Routing Table be built...(1-a) (2-b) : "); scanf("%d",&id); id--; adc=0; printf("\nNeighbours for Particular Node : "); for(i=0;i<no;i++) { if(graph[id][i]==1) { ad[adc]=i; adc++; printf("%c",nodes[i]);
Srinivasa Institute of Engineering & Technology Cheyyeru
Sheet
No:
} } for(i=0;i<no;i++) { if(id!=i) { small=100; chosen=-1; for(j=0;j<adc;j++) { int total=e[ad[j]][i]+e[id][ad[j]]; if(total<small) { small=total; chosen=j; } } e[id][i]=small; printf("\n\nShortest Estimate to %c is printf("\nNext hop is %c",nodes[ad[chosen]]); } else e[id][i]=0; } break; case 2: printf("\n"); for(i=0;i<no;i++) { for(j=0;j<no;j++) printf("%d",e[i][j]); printf("\n"); } break; } printf("\n\nDo you want to continue...(1-yes,2-no) : "); scanf("%d",&ch2); }while(ch2==1); return 0;
%d",nodes[i],small);
-:Output:Enter No.of Nodes : 4 Enter the Adjacency Matrix ------------------------------------Enter values for 1,1 Position : 0 Enter values for 1,2 Position : 1
Srinivasa Institute of Engineering & Technology Cheyyeru
2
hop1
Sheet
No:
Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter
values values values values values values values values values values values values values values
for for for for for for for for for for for for for for
1,3 1,4 2,1 2,2 2,3 2,4 3,1 3,2 3,3 3,4 4,1 4,2 4,3 4,4
Position Position Position Position Position Position Position Position Position Position Position Position Position Position
: : : : : : : : : : : : : :
1 0 1 0 0 1 1 0 0 1 0 1 1 0
hop0
hop3
hop2
Enter Initial Estimates _______________________ Estimate for Node-a to Node-a : 0 to Node-b : 1 to Node-c : 2 to Node-d : 3 Estimate for Node-b to Node-a : 2 to Node-b : 0 to Node-c : 4 to Node-d : 1 Estimate for Node-c to Node-a : 3 to Node-b : 1 to Node-c : 0 to Node-d : 2 Estimate for Node-d to Node-a : 5 to Node-b : 1 to Node-c : 2 to Node-d : 0 -:Menu:__________________________ 1.Routing Information for Node 2.Estimated Table Select any one from the above Options : 1 Which Node should Routing Table be built...(1-a)(2-b) : 1 Neighbours for Particular Node : bc
Srinivasa Institute of Engineering & Technology Cheyyeru
Sheet
No:
Shortest Estimate to b is 1 Next hop is b Shortest Estimate to c is 2 Next hop is c Shortest Estimate to d is 2 Next hop is b Do you want to continue...(1-yes,2-no) : 1 -:Menu:_________________________ 1.Routing Information for Node 2.Estimated Table Select any one from the above Options : 2 0 2 3 5 1 0 1 1 2 3 0 2 2 1 2 0