Professional Documents
Culture Documents
S.No. Practical
1.
Date
Remark
Implementation of the Data Link Layer framing method such as character stuffing and bit stuffing in C.
2.
3.
Implementation of a Hamming (7,4) code to limit the noise. We have to code the 4 bit data in to 7 bit data by adding 3 parity bits. Implementation will be in C.
4.
5.
6.
7.
Program-1
Implementation of the Data Link Layer framing method such as character stuffing and bit stuffing in C.
else { r[0]=a[i]; r[1]='\0'; strcat(fs,r); } p=0; } for(q=i;q<strlen(a);q++) { t[p++]=a[q]; } t[p]='\0'; strcat(fs,t); } strcat(fs,"01111110"); printf("After stuffing : %s",fs); getch(); }
Enter bit string : 10101111110 After stuffing : 0111111010101111101001111110 Enter bit string : 1011111011110111110 After stuffing : 0111111010111110011110111110001111110
Program-2
Implementation of CRC algorithm in C.
#include< stdlib.h> #include< conio.h> #include< stdio.h> void main() { int i,j,n,g,a,arr[20],gen[20],b[20],q[20],s; clrscr(); printf("Transmitter side:"); printf("\nEnter no. of data bits:"); scanf("%d",&n); printf("Enter data:"); for(i=0;i< n;i++) scanf("%d",&arr[i]);
printf("Enter size of generator:"); scanf("%d",&g); do{ printf("Enter generator:"); for(j=0;j< g;j++) scanf("%d",&gen[j]);
for(i=0;i< a;++i)
printf("%d",arr[i]);
for(i=0;i< n;++i) { if(arr[i]==0) { for(j=i;j< g+i;++j) arr[j] = arr[j]^0; } else { arr[i] = arr[i]^gen[0]; arr[i+1]=arr[i+1]^gen[1]; arr[i+2]=arr[i+2]^gen[2]; arr[i+3]=arr[i+3]^gen[3]; } } printf("\n\tThe CRC is :"); for(i=n;i < a;++i) printf("%d",arr[i]); s=n+a; for(i=n;i< s;i++) q[i]=arr[i]; printf("\n"); for(i=0;i< a;i++) printf("%d",q[i]); getch(); }
Output
Transmitter side: Enter no. of data bits:8 Enter data:1 0 1 0 0 0 0 1 Enter size of generator:4 Enter generator:1 0 0 1 The generator matrix:1001 The appended matrix is:10100001000 The CRC is :111 10100001111
Program-3
Implementation of a Hamming (7,4) code to limit the noise. We have to code the 4 bit data in to 7 bit data by adding 3 parity bits.Implementation will be in C.
#include<stdio.h> #include<stdlib.h> int main() { int e[7],i,j; for(i=0;i<7;i++) e[i]=0; printf("\n enter the 4 bits\n"); for(i=3;i<7;i++) scanf("%d",&e[i]); e[0]=(e[4]+e[5]+e[6])%2; e[1]=(e[3]+e[5]+e[6])%2; e[2]=(e[3]+e[4]+e[6])%2; for(i=0;i<7;i++) printf("%d",e[i]); }
OUTPUT
enter the 4 bits 1010
0101010
Program-4
Implementation of LZW compression algorithm in C.
#include <stdio.h> #include <stdlib.h> #include <string.h>
#define BITS 12
#define MAX_VALUE (1 << BITS) - 1 /* Note that MS-DOS machines need to */ #define MAX_CODE MAX_VALUE - 1 /* 14 bits are selected. #if BITS == 14 #define TABLE_SIZE 18041 #endif #if BITS == 13 /* The string table size needs to be a */ /* compile their code in large model if*/ */
#define TABLE_SIZE 9029 #endif #if BITS <= 12 #define TABLE_SIZE 5021 #endif
void *malloc(); int *code_value; unsigned int *prefix_code; /* This is the code value array */
unsigned char *append_character; /* This array holds the appended chars */ unsigned char decode_stack[4000]; /* This array holds the decoded string */ /* * Forward declarations */ void compress(FILE *input,FILE *output); void expand(FILE *input,FILE *output); int find_match(int hash_prefix,unsigned int hash_character); void output_code(FILE *output,unsigned int code);
unsigned int input_code(FILE *input); unsigned char *decode_string(unsigned char *buffer,unsigned int code); /******************************************************************** ** ** This program gets a file name from the command line. It compresses the ** file, placing its output in a file named test.lzw. It then expands ** test.lzw into test.out. Test.out should then be an exact duplicate of ** the input file. ** *************************************************************************/ main(int argc, char *argv[]) { FILE *input_file; FILE *output_file; FILE *lzw_file; char input_file_name[81]; /* ** The three buffers are needed for the compression phase. */ code_value=(int*)malloc(TABLE_SIZE*sizeof(int)); prefix_code=(unsigned int *)malloc(TABLE_SIZE*sizeof(unsigned int)); append_character=(unsigned char *)malloc(TABLE_SIZE*sizeof(unsigned char)); if (code_value==NULL || prefix_code==NULL || append_character==NULL) { printf("Fatal error allocating table space!\n"); exit(-1); } /* ** Get the file name, open it up, and open up the lzw output file. */ if (argc>1) strcpy(input_file_name,argv[1]); else
{ printf("Input file name? "); scanf("%s",input_file_name); } input_file=fopen(input_file_name,"rb"); lzw_file=fopen("test.lzw","wb"); if (input_file==NULL || lzw_file==NULL) { printf("Fatal error opening files.\n"); exit(-1); }; /* ** Compress the file. */ compress(input_file,lzw_file); fclose(input_file); fclose(lzw_file); free(code_value); /* ** Now open the files for the expansion. */ lzw_file=fopen("test.lzw","rb"); output_file=fopen("test.out","wb"); if (lzw_file==NULL || output_file==NULL) { printf("Fatal error opening files.\n"); exit(-2); }; /* ** Expand the file. */ expand(lzw_file,output_file); fclose(lzw_file); fclose(output_file);
free(prefix_code); free(append_character); } /* ** This is the compression routine. The code should be a fairly close ** match to the algorithm accompanying the article. ** */ void compress(FILE *input,FILE *output) { unsigned int next_code; unsigned int character; unsigned int string_code; unsigned int index; int i; next_code=256; /* Next code is the next available string code*/
for (i=0;i<TABLE_SIZE;i++) /* Clear out the string table before starting */ code_value[i]=-1; i=0; printf("Compressing...\n"); string_code=getc(input); /* ** This is the main loop where it all happens. This loop runs util all of ** the input has been exhausted. Note that it stops adding codes to the ** table after all of the possible codes have been defined. */ while ((character=getc(input)) != (unsigned)EOF) { if (++i==1000) { i=0; printf("*"); } /* Print a * every 1000 /* input characters. This */ /* is just a pacifier. */ */ /* Get the first code */
index=find_match(string_code,character);/* See if the string is in */ if (code_value[index] != -1) string_code=code_value[index]; else { /* the table. If it is, */ /* get the code value. If */
if (next_code <= MAX_CODE) { code_value[index]=next_code++; prefix_code[index]=string_code; append_character[index]=character; } output_code(output,string_code); /* When a string is found */ string_code=character; } } /* ** End of the main loop. */ output_code(output,string_code); /* Output the last code */ */ /* that is not in the table*/
output_code(output,MAX_VALUE); /* Output the end of buffer code output_code(output,0); printf("\n"); } /* This code flushes the output buffer*/
/* ** This is the hashing routine. It tries to find a match for the prefix+char ** string in the string table. If it finds it, the index is returned. If ** the string is not found, the first available index in the string table is ** returned instead. */ int find_match(int hash_prefix,unsigned int hash_character) { int index; int offset;
index = (hash_character << HASHING_SHIFT) ^ hash_prefix; if (index == 0) offset = 1; else offset = TABLE_SIZE - index; while (1) { if (code_value[index] == -1) return(index); if (prefix_code[index] == hash_prefix && append_character[index] == hash_character) return(index); index -= offset; if (index < 0) index += TABLE_SIZE; } } /* ** This is the expansion routine. It takes an LZW format file, and expands ** it to an output file. The code here should be a fairly close match to ** the algorithm in the accompanying article. */ void expand(FILE *input,FILE *output) { unsigned int next_code; unsigned int new_code; unsigned int old_code; int character; int counter; unsigned char *string;
next_code=256;
counter=0;
*/
printf("Expanding...\n");
old_code=input_code(input); /* Read in the first code, initialize the */ character=old_code; putc(old_code,output); /* ** This is the main expansion loop. It reads in characters from the LZW file ** until it sees the special code used to inidicate the end of the data. */ while ((new_code=input_code(input)) != (MAX_VALUE)) { if (++counter==1000) /* This section of code prints out { counter=0; printf("*"); } /* ** This code checks for the special STRING+CHARACTER+STRING+CHARACTER+STRING ** case which generates an undefined code. It handles it by decoding ** the last code, and adding a single character to the end of the decode string. */ if (new_code>=next_code) { *decode_stack=character; string=decode_string(decode_stack+1,old_code); } /* ** Otherwise we do a straight decode of the new code. */ else string=decode_string(decode_stack,new_code); /* /* an asterisk every 1000 characters */ /* It is just a pacifier. */ */ /* character variable, and send the first */ /* code to the output file */
** Now we output the decoded string in reverse order. */ character=*string; while (string >= decode_stack) putc(*string--,output); /* ** Finally, if possible, add a new code to the string table. */ if (next_code <= MAX_CODE) { prefix_code[next_code]=old_code; append_character[next_code]=character; next_code++; } old_code=new_code; } printf("\n"); } /* ** This routine simply decodes a string from the string table, storing ** it in a buffer. The buffer can then be output in reverse order by ** the expansion program. */ unsigned char *decode_string(unsigned char *buffer,unsigned int code) { int i;
{ printf("Fatal error during code expansion.\n"); exit(-3); } } *buffer=code; return(buffer); } /* ** The following two routines are used to output variable length ** codes. They are written strictly for clarity, and are not ** particularyl efficient. */ unsigned int input_code(FILE *input) { unsigned int return_value; static int input_bit_count=0; static unsigned long input_bit_buffer=0L;
while (input_bit_count <= 24) { input_bit_buffer |= (unsigned long) getc(input) << (24-input_bit_count); input_bit_count += 8; } return_value=input_bit_buffer >> (32-BITS); input_bit_buffer <<= BITS; input_bit_count -= BITS; return(return_value); } void output_code(FILE *output,unsigned int code) { static int output_bit_count=0; static unsigned long output_bit_buffer=0L;
output_bit_buffer |= (unsigned long) code << (32-BITS-output_bit_count); output_bit_count += BITS; while (output_bit_count >= 8) { putc(output_bit_buffer >> 24,output); output_bit_buffer <<= 8; output_bit_count -= 8; } }
Output
Input String = /WED/WE/WEE/WEB/WET Character Input Code Output New code value New String /W E D / WE / WEE /W EB / WET EOF / W E D 256 E 260 261 257 B 260 T 256 257 258 259 260 261 262 263 264 265 266 /W WE ED D/ /WE E/ /WEE E/W WEB B/ /WET
Input Codes: / W E D 256 E 260 261 257 B 260 T Input/ NEW_CODE / W E D 256 E 260 261 257 B 260 T OLD_CODE / / W E D 256 E 260 261 257 B 260 STRING/ CHARACTER New table entry Output / W E D /W E /WE E/ WE B /WE T W E D / E / E W B / T 256 = /W 257 = WE 258 = ED 259 = D/ 260 = /WE 261 = E/ 262 = /WEE 263 = E/W 264 = WEB 265 = B/ 266 = /WET
Program-5
Write a socket program in C to implement a listener and a talker.
These two programs show how you can establish a socket connection using the above functions.
socket_server.c
#include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <stdio.h> #define NSTRS #define ADDRESS /* * Strings we send to the client. */ char *strs[NSTRS] = { "This is the first string from the server.\n", "This is the second string from the server.\n", "This is the third string from the server.\n" }; main() { char c; FILE *fp; int fromlen; register int i, s, ns, len; struct sockaddr_un saun, fsaun; /* * Get a socket to work with. This socket will * be in the UNIX domain, and will be a * stream socket. */ if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { 3 /* no. of strings */ "mysocket" /* addr to connect */
perror("server: socket"); exit(1); } /* * Create the address we will be binding to. */ saun.sun_family = AF_UNIX; strcpy(saun.sun_path, ADDRESS); /* * Try to bind the address to the socket. We * unlink the name first so that the bind won't * fail. * * The third argument indicates the "length" of * the structure, not just the length of the * socket name. */ unlink(ADDRESS); len = sizeof(saun.sun_family) + strlen(saun.sun_path); if (bind(s, &saun, len) < 0) { perror("server: bind"); exit(1); } /* * Listen on the socket. */ if (listen(s, 5) < 0) { perror("server: listen"); exit(1); } /* * Accept connections. When we accept one, ns * will be connected to the client. fsaun will * contain the address of the client.
*/ if ((ns = accept(s, &fsaun, &fromlen)) < 0) { perror("server: accept"); exit(1); } /* * We'll use stdio for reading the socket. */ fp = fdopen(ns, "r"); /* * First we send some strings to the client. */ for (i = 0; i < NSTRS; i++) send(ns, strs[i], strlen(strs[i]), 0); /* * Then we read some strings from the client and * print them out. */ for (i = 0; i < NSTRS; i++) { while ((c = fgetc(fp)) != EOF) { putchar(c); if (c == '\n') break; } } /* * We can simply use close() to terminate the * connection, since we're done with both sides. */ close(s); exit(0); }
socket_client.c
#include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <stdio.h> #define NSTRS #define ADDRESS /* * Strings we send to the server. */ char *strs[NSTRS] = { "This is the first string from the client.\n", "This is the second string from the client.\n", "This is the third string from the client.\n" }; main() { char c; FILE *fp; register int i, s, len; struct sockaddr_un saun; /* * Get a socket to work with. This socket will * be in the UNIX domain, and will be a * stream socket. */ if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { perror("client: socket"); exit(1); } /* * Create the address we will be connecting to. 3 /* no. of strings */ "mysocket" /* addr to connect */
*/ saun.sun_family = AF_UNIX; strcpy(saun.sun_path, ADDRESS); /* * Try to connect to the address. For this to * succeed, the server must already have bound * this address, and must have issued a listen() * request. * * The third argument indicates the "length" of * the structure, not just the length of the * socket name. */ len = sizeof(saun.sun_family) + strlen(saun.sun_path); if (connect(s, &saun, len) < 0) { perror("client: connect"); exit(1); } /* * We'll use stdio for reading * the socket. */ fp = fdopen(s, "r"); /* * First we read some strings from the server * and print them out. */ for (i = 0; i < NSTRS; i++) { while ((c = fgetc(fp)) != EOF) { putchar(c); if (c == '\n') break; } }
/* * Now we send some strings to the server. */ for (i = 0; i < NSTRS; i++) send(s, strs[i], strlen(strs[i]), 0); /* * We can simply use close() to terminate the * connection, since we're done with both sides. */ close(s); exit(0); }
OUTPUT
We can simply use close () to terminate the connection, since we're done with both sides.
Program-6
Program to find shortest path routing between n nodes(Dijkstras Algorithm).
#include<stdio.h> #include<stdlib.h> void sort(void); static int dsp[10][10],nodes; struct{ char src; char dest; int length; }stemp,permanent[10]={' ',' ',0},temp[10]={' ',' ',-1}; static int perm,tem; void main() { int i,j,k,l,m,n=0,point; char initial,dest,path[10]={' '}; clrscr(); printf("\t\t Shortest Path (Dijkstra's algorithm)"); printf("\n*******************************************************"); printf( \nEnter the number of nodes:); scanf(%d,&nodes); printf (\nEnter the adjacency matrix for the graph:\n); for(i=0;i<nodes;i++) { for(j=0;j<nodes;j++) scanf(%d,&dsp[I][j]); } fflush(stdin); printf("\n enter the source node:"); scanf("%c",&initial);fflush(stdin); printf("\n Enter the destination node:"); scanf("%c",&dest);
permanent[perm].src=initial; permanent[perm].dest=initial; permanent[perm++].length=0; i=permanent[perm-1].dest-97; for(j=0;j<nodes;j++) { if(i!=j) { if(dsp[i][j]>0) { temp[tem].src=permanent[perm-1].src; temp[tem].dest=j+97; temp[tem++].length=dsp[i][j]; } } } sort(); while(tem>=0) { j=permanent[perm-1].dest-97; for(i=0;i<nodes;i++) { if(i!=initial-97) { if(dsp[j][i]>0) { l=-1; for(k=0;k<perm;k++) { if(permanent[k].dest==(i+97)) l=k; } for(k=0;k<=tem;k++) {
if(temp[k].dest==(i+97)) l=k; } if(l<0) { temp[tem].src=j+97; temp[tem].dest=i+97; for(m=0;m<perm;m++) { if(permanent[m].dest==temp[tem].src) n=permanent[m].length; } temp[tem++].length=dsp[j][i]+n; } else { for(m=0;m<perm;m++) { if(permanent[m].dest==j+97) { n=permanent[m].length+dsp[j][i];break; } else n=dsp[j][i]; } if((n<temp[l].length)) { temp[l].length=n; temp[l].src=j+97; temp[l].dest=i+97; } } } }
} sort(); } printf("\nShortest path:\n"); printf("From %c to %c is:",initial,dest); for(i=0;i<perm-1;i++) { if(permanent[i].dest==dest) { point=i;n=i; } } i=0; for(j=perm;j>0;j--) { if(permanent[j-1].dest==permanent[point].src) { path[i++]=permanent[point].dest; point=j-1; } } path[i]=initial; for(j=i;j>=0;j--) printf("%c ",path[j]); printf("\t length=%d",permanent[n].length); getch(); } break;
{ if((temp[j].length <= temp[j+1].length)) { stemp=temp[j]; temp[j]=temp[j+1]; temp[j+1]=stemp; } } if(k) break; } permanent[perm++]=temp[tem-1]; temp[tem-1].src=' ';temp[tem-1].dest=' '; temp[tem-1].length=-1; tem--; } k=0;
Network topology:
-----------------------1 a 2 d b 1 2 1 c 1 e 3 f
Output of execution1:
----------------------------
-1 -1
Enter the source node:a Enter the destination node:f Shortest path: From a to f is:a b c e f length=6
7 2 E 1 4 2 F 2 3
C 3 D 2 H
-1 2 -1 -1 0 2 1 -1 -1 -1 3 -1 2 0 -1 2 6 -1 -1 -1 1 -1 0 4 -1 -1 -1 2 -1 2 4 0
Enter the source node:a Enter the destination node:d Shortest path: From a to d is:a b e f h d length=10
Program-7
Implementation of RSA Algorithm in C. /********** RSA PROGRAM ***********/ /*d value should be less than 11 bcoz (c^d)modn can't be computed using available datatypes*/
#include<stdio.h> #include<string.h> #include<math.h> void main() { char a[]={"0ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; int n,i,j,s,n2,k1,p,q,d,m1,e1,l5,z,p2[30],s1,c[30]; unsigned long int l3,m,l4,k2; double l2,l1,l6; float e,l; char p1[30]; clrscr(); printf("enter two prime numbers p and q\n"); scanf("%d %d",&p,&q); do{ n=p*q; if(n<26) { printf("\n n value is not large enough.\nplease select p, q value such that p*q is greater than 26"); scanf("%d %d",&p,&q);} } while(n<26); z=((p-1)*(q-1)); printf("enter the value of d:\n"); scanf("%d",&d); for(j=1;j<z;j++) {
printf("ENCRYPTION-CIPHERTEXT"); printf("enter the plain text\n"); scanf("%s",p1); for(i=0;i<strlen(p1);i++) { for(j=1;j<strlen(a);j++) { if(a[j]==p1[i]) { s=j; break; } else continue; } printf("%d",s); e1=(int)e; l1=pow(((double)s),((double)e1)); k2=fmod(l1,(double)n); printf("\n%lu\n",k2); c[i]=(int)k2; printf("cipher:%d\n",c[i]); } printf("\n"); for(i=0;i<strlen(p1);i++) { l2=(pow(((double)c[i]),((double)d))); m=fmod(l2,(double)n); m1=(int)m;
RSA OUTPUT
Enter two prime numbers p and q 3 11 enter the value of d: 7 33 20 3.000000
ENCRYPTION-CIPHERTEXTenter the plain text SUZANNE 19 28 cipher:28 21 21 cipher:21 26 20 cipher:20 1 1 cipher:1 14 5 cipher:5 14 5 cipher:5 5 26 cipher:26
S U
Z A N N E
RSA OUTPUT:
Enter two prime numbers p and q 5 13 enter the value of d: 7 65 48 7.000000
ENCRYPTION-CIPHERTEXTenter the plain text NAINA 14 14 cipher:14 1 1 cipher:1 9 9 cipher:9 14 14 cipher:14 1 1 cipher:1
N A I N A