postheadericon Checksum - Error Detection and Correction Techniques


checksum.h


//checksum header file
#include<stdio.h>
#include<string.h>
# define segment_len 8
# define no_segments 4
# define frameSize 32
int getBit(int frameData,int pos)
{
int code=1; //base
code=code<<(pos-1);

if(frameData & code)
return 1;
else
return 0;
}

void sum(char segment1[],char segment2[])
{
int i,j;
//int segment_len=strlen(segment1);
int ans=0,carry=0;
char ans_segment[9]=" ";
// segment_len--;//because starting from zero

for(i=segment_len-1;i>=0;i--)
{
ans=0;
ans=( (int)segment1[i]-48 ) + ( (int)segment2[i]-48 ) + carry; //sum of bits on each position of two segments

if(ans>=2)//carry genrated
{
carry=1;
if(ans==2)
ans=0; // 1 + 1 = 10 ans
else
ans=1;//  1 + 1 + 1(carry) = 11 ans
}
else
{
carry=0;
}
ans_segment[i]=ans+48;

}
ans_segment[segment_len+1]='\0';
strcpy(segment1,ans_segment);

if(carry==0)
return;
else
sum(segment1,"00000001"); //recursive call of function if carry genrated
}


void complement(char segment[])
{
int i,len=strlen(segment);
for(i=0;i<len;i++)
{
if(segment[i]=='1')
segment[i]='0';
else
segment[i]='1';
}
segment[i]='\0';
}




Sender / Generate Checksum


//Sender

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include "checksum.h"

void main()
{
int pid,i,k,j;
int frameData;
char myFrame[50];
char segments[5][10];


system(">pipe");
pid =open("pipe",O_WRONLY);
system("clear");

//enter frame data in decimal no.
printf("\nEnter frame data : ");
scanf("%d",&frameData);
//generate bit
for(i=frameSize;i>0;i--)
{
myFrame[k++]=getBit(frameData,i)+48;
}
myFrame[k]='\0';

printf("\nOriginal Data : %s",myFrame);
printf("\nData in Segments____________________________________\n");

//printf segments

k=0;
for(i=0;i<no_segments;i++)
{
for(j=0;j<segment_len;j++)
{
segments[i][j]=myFrame[k++];
}
segments[i][j]='\0';
printf("\n\tSegments[%d] : %s",i,segments[i]);
}
printf("\n____________________________________________________\n");
strcpy(segments[no_segments],segments[0]);
for(i=1;i<no_segments;i++)
{
sum(segments[no_segments],segments[i]);
}
printf("\n\tFinal Sum   : %s\n",segments[no_segments]);
complement(segments[no_segments]);
printf("\n\tCheck sum   : %s (Complement of Final sum)\n",segments[no_segments]);

write(pid,&segments,sizeof(segments));
close(pid);
}

Reciver


//Reciver

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include "checksum.h"


void main()
{
int pid,i,k,j;
int frameData;
char myFrame[50];
char segments[5][10];
char p_checksum[9];//previously written checksum



pid =open("pipe",O_RDONLY);
read(pid,&segments,sizeof(segments));
system("clear");

printf("\nData in Segments__________________________\n");


//no_segments=4;
k=0;
for(i=0;i<no_segments;i++)
{
printf("\n\tSegments[%d] : %s",i,segments[i]);
}
strcpy(p_checksum,segments[no_segments]);

printf("\nData in Segments__________________________\n");
//calculate checksum at receiver end
strcpy(segments[no_segments],segments[0]);
for(i=1;i<no_segments;i++)
{
sum(segments[no_segments],segments[i]);
}
printf("\nFinal Sum : %s\n",segments[no_segments]);

sum(p_checksum,segments[no_segments]);
complement(p_checksum);
printf("\nCheck Sum : %s (Complement of final sum)\n",p_checksum);

if(strcmp(p_checksum,"00000000")==0)
printf("\nData Accepted\n");
else
printf("\nData Rejected\n");

}


0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.