Checksum sender.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
unsigned char getCheckSum(unsigned char a,unsigned b){
char flag=0;
unsigned char carry=a&b;
unsigned char result=a^b;
unsigned char carryout=0;
while(carry!=0){
if(carry>=128)
flag=1;
carryout =carry<<1;
carry=result & carryout;
result=result^carryout;
}
if(flag==1){
result=getCheckSum(result,1);
}
return result;
}
int main(){
int i,pid;
pid=open("MyPipe",O_WRONLY);
unsigned char data[10]={'\0'};
unsigned char chksum='\0';
system("clear");
printf("Enter data");
scanf("%s" ,data);
chksum=data[0];
for(i=1;data[i];i++){
chksum=getCheckSum(chksum,data[i]);
}
printf("%d = %d = %c",chksum,~chksum,~chksum);
data[i]=~chksum;
data[i+1]='\0';
printf("chksum = %d Sending is %s ",
data[strlen(data)-1],data);
data[strlen(data)-1],data);
write(pid,data,sizeof(data));
}
rece.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
unsigned char getCheckSum(unsigned char a,unsigned b){
char flag=0;
unsigned char carry=a&b;
unsigned char result=a^b;
unsigned char carryout=0;
while(carry!=0){
if(carry>=128)
flag=1;
carryout =carry<<1;
carry=result & carryout;
result=result^carryout;
}
if(flag==1){
result=getCheckSum(result,1);
}
return result;
}
int main(){
int i,pid;
//
unsigned char data[10]={'\0'},string[10]={'\0'};
unsigned char chksum='\0';
int len;
unsigned char send_chksum='\0';
system("clear");
mkfifo("MyPipe",0666);
pid=open("MyPipe",O_RDONLY);
read(pid,data,sizeof(data));
send_chksum=data[strlen(data)-1];
len=strlen(data);
printf("received data is %s and length is %d,chksum =%d",
data,len,data[strlen(data)-1]);
data,len,data[strlen(data)-1]);
for(i=0;strlen(data)-1;i++){
chksum=getCheckSum(chksum,data[i]);
string[i]=data[i];
}
chksum=~chksum;
printf("\nSending is %s ",string);
if(send_chksum==chksum){
printf("\ndata is accurate");
}else{
printf("\ndata is not accurate");
}
unlink("MyPipe");
return 0;
}
Post a Comment