Вы находитесь на странице: 1из 18

COMPUTER NETWORK LAB

Page | 1

INDEX
Sl.no. Name of program Date Page no. Signature

Page | 2

1. C program to implement bit stuffing. Program #include <stdio.h> #include <string.h> void stuff(char str[40]); main() { char str[40]; int i; printf("\n enter the string:"); scanf("%s", &str); printf("\n the given string is:"); printf("\n %s", str); stuff(str); return 0; } void stuff(char str[40]) { int i, j, k = 0, l, n, z; printf("\n now we are stuffing the data::\n"); n = strlen(str); for (i = 0; i < n; i++) { if (str[i] == '1') { k = 1; for (l = i + 1; l <= i + 5; l++) { if (str[l] == '1') k++; else break; } if (k == 6) { i = i + 6; z = n + 1; for (j = z; j >= i; j--) { str[j] = str[j - 1]; } str[j] = '0'; } } } printf("\nThe resultant string after stuffing is..\n"); printf("%s\n", str); } Page | 3

Output Enter the string:111111111 The resultant string after stuffing is .. 1111101111

Page | 4

2. C program to implement byte stuffing. Program #include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> void main() { int i=0,j=0,n,pos; char a[20],b[50],ch; clrscr(); printf("enter string\n"); scanf("%s",&a); n=strlen(a); printf("enter position\n"); scanf("%d",&pos); if(pos>n) { printf("invalid position, Enter again :"); scanf("%d",&pos); } printf("enter the character\n"); 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; } Page | 5

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:\n"); printf("%s",b); getch(); } Output enter the string abhdlefghtdle enter position 4 enter the character u frame after stuffing: dlestxabhdleudledledlefghtdledledleetx

Page | 6

3. C program to implement CRC algorithm. Program #include <stdio.h> #include <stdlib.h> void main() { int i, j, n, g, a, ms[20], gen[20], b[20], q[20], s; printf("transmiter side"); printf("enter no of data bits"); scanf("%d", &n); printf("enter data"); for (i = 0; i < n; i++) scanf("%d", &ms[i]); printf("enter size of generator"); scanf("%d", &g); printf("enter generator"); for (j = 0; j < g; j++) scanf("%d", &gen[j]); printf("\n \t the generated matrix"); for (j = 0; j < g; j++) printf("%d", gen[j]); a = n + (g - 1); printf("\t \n the appended matrix is::"); for (i = 0; i < j; i++) ms[n + i] = 0; for (i = 0; i < a; i++) printf("%d", ms[i]); for (i = 0; i < n; i++) q[i] = ms[i]; for (i = 0; i < n; i++) { if (ms[i] == 0) { for (j = i; j < g + i; j++) { ms[j] = ms[j] ^ 0; } } else { ms[i] = ms[i] ^ gen[0]; ms[i + 1] = ms[i + 1] ^ gen[1]; ms[i + 2] = ms[i + 2] ^ gen[2]; ms[i + 3] = ms[i + 3] ^ gen[3]; } } printf("\n \t the crc is::"); for (i = n; i < a; i++) printf("%d", ms[i]); Page | 7

s = n + a; for (i = n; i < s; i++) q[i] = ms[i]; printf("\n"); for (i = 0; i < a; i++) printf("%d", q[i]); getch(); }

Output transmitter side enter no. of data bits 4 enter data 1 0 0 1 enter size of generator 4 enter generator 1 0 1 1 the appended matrix is:: 1001000 the crc is:: 110 1001110

Page | 8

4. C program to implement Dijkstras shortest path algorithm. Program #include<stdio.h> #include<conio.h> #define infinity 999 void dij(int n,int v,int cost[10][10],int dist[]); void main() { int n,v,i,j,cost[10][10],dist[10]; clrscr(); printf("\n Enter the number 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; } printf("\n Enter the source matrix:"); scanf("%d",&v); dij(n,v,cost,dist); printf("\n Shortest path:\n"); for(i=1;i<=n;i++) if(i!=v) printf("%d->%d,cost=%d\n",v,i,dist[i]); getch(); } void dij(int n,int v,int cost[10][10],int dist[]) { int i,u,count,w,flag[10],min; for(i=1;i<=n;i++) flag[i]=0,dist[i]=cost[v][i]; count=2; while(count<=n) { min=99; for(w=1;w<=n;w++) if(dist[w]<min && !flag[w]) min=dist[w],u=w; flag[u]=1; count++; for(w=1;w<=n;w++) if((dist[u]+cost[u][w]<dist[w]) && !flag[w]) dist[w]=dist[u]+cost[u][w]; } }

Page | 9

Output Enter the number of nodes: 3 Enter the cost matrix: 1 2 3 4 2 3 1 5 2 Enter the source: 3 Shortest path: 3->1, cost=1 3->2, cost=3

Page | 10

5. C program to implement Hamming (7, 4) code to limit the noise. Program #include<stdio.h> #include<conio.h> void main() { int d[4],c[7],i; clrscr(); printf("\n Enter the 4 bit date seperated by blanks\n"); for(i=0;i<4;i++) scanf("%d",&d[i]); for(i=0;i<4;i++) c[i]=d[i]; c[4]=(d[0]+d[2]+d[3])%2; c[5]=(d[0]+d[1]+d[3])%2; c[6]=(d[1]+d[2]+d[3])%2; printf("\n The data bit appended with correction bit is \n"); for(i=0;i<7;i++) printf("%d",c[i]); gettch(); } Output Enter the 4 bit data separated by blanks 1 0 1 0 The data bit appended with correction bit is 1010011

Page | 11

6. C program to implement distance vector routing. Program #include<stdio.h> #include<conio.h> struct node { unsigned dist[20]; unsigned from[20]; }rt[10]; void main() { int dmat[20][20]; int n,i,j,k,count=0; printf("\nEnter the number of nodes : "); scanf("%d",&n); printf("\nEnter the cost matrix :\n"); 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]>dmat[i][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\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]); } } printf("\n\n"); getch(); } Page | 12

Output Enter the no. of nodes:3 Enter the cost matrix: 1 2 3 2 3 1 5 4 2 State value for router 1 is node 1 via 1 Distance0 node 2 via 2 Distance2 node 3 via 3 Distance3

Page | 13

7. C program to implement Bellman ford algorithm. Program #include<stdio.h> #include<conio.h> #define MAX 100 #define EF 1000 #define NIL -1 #define TRUE 1 #define FALSE 0 int graph[MAX][MAX][2],d[MAX],pi[MAX],nn,en; void ISS(int s); void R(int u,int v); int Bellman_Ford(int s); void PrintPath(int s, int v); int main(int argc, char *argv[]) { int i,j,k,weight,c,s,v; printf("\nzEnter the number of nodes to use in the graph : "); scanf("%d",&nn); printf("\nEnter the number of edges present in the graph : "); scanf("%d",&en); printf("\nNow enter %d number of edges(pairs of 2 nodes)\nAlong with their weights one by one\nAn edge should be entered only once\n",en); for(i=0;i<en;i++) { printf("\nPair no %d and its weight (U V W) : ",i+1); scanf("%d %d %d",&j,&k,&weight);j--;k--; graph[j][k][0]=1;graph[j][k][1]=weight; } printf("\nA graph having %d nodes & %d edges has been created\n",nn,en); printf("\nSome info. of the graph is printed bellow : \n"); for(i=0;i<nn;i++) { c=1; printf("\nVisitable nodes and their weights from %d no node are given bellow : \n",i+1); for(j=0;j<nn;j++) if(graph[i][j][0]==1) { printf("(%d) no visitable node = %d and weight =%d\n",c,j+1,graph[i][j][1]);c++;} } Page | 14

printf("\nNow press any key to start calculation on the graph\nHere a single source is taken and shortest path is calculated\n"); getch(); do{ printf("\nNow enter the number of the source node : "); scanf("%d",&s); c=Bellman_Ford(s-1); if(c==TRUE)printf("\nHere Single Source Shortest Path Algorithm is logically applicable\n"); else printf("\nHere Single Source Shortest Path Algorithm is logically not applicable\n"); printf("\nNow enter the number of the destination node : "); scanf("%d",&v); printf("\nThe shortest path from node (%d) to node (%d) is printed bellow serially : \n\n",s,v); PrintPath(s-1,v-1); printf("\n\nAnd the distanse between the two nodes is %d",d[v-1]); printf("\n\nEnter 1 to continue calculation\nOr 2 to end calculation\n\nEnter your choice : "); scanf("%d",&c); }while(c!=2); printf("\nNow press any key to finish....."); getch(); return 0; } void ISS(int s) { int i; for(i=0;i<nn;i++) { d[i]=EF; pi[i]=NIL; } d[s]=0; } void R(int u,int v) { if(d[v]>(d[u]+graph[u][v][1])) { d[v]=(d[u]+graph[u][v][1]); pi[v]=u; } }

Page | 15

int Bellman_Ford(int s) { int i,j,k; ISS(s); for(i=0;i<(nn-1);i++) { for(j=0;j<nn;j++) for(k=0;k<nn;k++) if(graph[j][k][0]==1) R(j,k); } for(i=0;i<nn;i++) for(j=0;j<nn;j++) if(graph[i][j][0]==1) { if(d[j]>(d[i]+graph[i][j][1])) return FALSE; } return TRUE; } void PrintPath(int s, int v) { if(v==s)printf(" %d ",s+1); else if(pi[v]==NIL) printf("No path from %d to %d exists",s+1,v+1); else { PrintPath(s,pi[v]); printf(" %d ",v+1); } }

Output

Enter the number of nodes to be used in the graph:5 Enter the number of edges present in the graph:8 Now enter 8 edges in pair with their weights: 1 2 -1 1 3 4 2 3 3 2 4 2 4 2 1 4 3 5 2 5 2 5 4 -3 Visitable node and their weights from 1 no node are given below: (1)no visitable node=2 and weight =-1 (2)no visitable node=5 and weight=4 Visitable node and their weights from 2 no node are given below: Page | 16

(1)no visitable node=3 and weight =3 (2)no visitable node=4 and weight=2 (3)no visitable node=5 and weight=2 Visitable node and their weights from 3 no node are given below: Visitable node and their weights from 4 no node are given below: (1)no visitable node=2 and weight =1 (2)no visitable node=3 and weight=5 Visitable node and their weights from 5 no node are given below: (1)no visitable node=4 and weight =-3

Page | 17

8. C program to obtain network id from IP and subsequent subnet mask. Program #include<stdio.h> #include<conio.h> void main() { int a,b,c,d; clrscr(); printf(Enter the IP in dotted decimal format with space for every dot:); scanf(%d%d%d%d,&a,&b,&c,&d); if(a<=127) printf(\nNetwork ID is:%d,a,\nSubnet is:255.0.0.0); else if(a>127&&a<=191) printf(\nNetwork ID is:%d.d,a,b,\nSubnet is:255.255.0.0); else if(a>191&&a<=233) printf(\nNetwork ID is:%d.%d.%d,a,b,c,\nSubnet is:255.255.255.0); else if(a>233) printf(\nMulticast IP or Reserved IP); getch(); } Output Enter the IP in dotted decimal format with space for every dot: 192.168.50.10 Network ID is:192.168.50 Subnet is:255.255.255.0

Page | 18

Вам также может понравиться