Subscribe to Get Free Material Updates!
Visit my new blog WebData Scraping - Web Scraping Service provider company in India.

Program to add the parity bit to frames at sender site & check at receiver side for correct recipient of data!


#include<stdio.h>
#include<string.h>
#include<ctype.h>

struct s
{
        int seq_no,frame[10],parity;
}s1[20],tmp[20];
void copy(struct s[]);
void parityCount(struct s[]);
void print(struct s[]);
void error(int *);
void sender(int *,int);
void reciver(struct s[]);
int main()
{
        char str[20];
        int i,j,as,q,arr[10],a[50],n,k;
        printf("Enter any string..");
        scanf("%s",str);
        n=0;
        printf("\n\n String in binary..\n\n");
        for(j=0;str[j];j++)
        {
                i=0;
                as=toascii(str[j]);
                q=as;
                while(q!=1)
                {
                        arr[i++]=q%2;
                        q/=2;
                }
                arr[i++]=1;
                for(k=i-1;k>=0;k--)
                        a[n++]=arr[k];
        }
        for(j=0;j<n;j++)
                printf("%d",a[j]);
        printf("\n");
        sender(a,n);
        return 0;
}
int len;
void sender(int a[],int i)
{
        int j,no=0,k;
        len=i/10;
        if(i%10==0)
                len--;
        for(j=0;j<=len;j++)
        {
                s1[j].seq_no=(j+1);
                for(k=0;k<10;k++)
                {
                        if(no!=i)
                                s1[j].frame[k]=a[no++];
                }
        }
        parityCount(s1);
        copy(s1);
        printf("\n Sending data..\n\n");
        print(s1);
        reciver(s1);
}
void copy(struct s temp[])  // After receiving check for correct frame. 
{
        int j,k;
        for(j=0;j<=len;j++)
        {
                tmp[j].seq_no=(j+1);
                for(k=0;k<10;k++)
                        tmp[j].frame[k]=s1[j].frame[k];
                tmp[j].parity=temp[j].parity;
        }
}
void parityCount(struct s *s1) // For count the parity bits of frame.
{
        int ans=0,j,k;
        for(j=0;j<=len;j++)
        {
                for(k=0;k<10;k++)
                        ans=ans ^ s1[j].frame[k];
                s1[j].parity=ans;
        }
}

void print(struct s s1[]) // Printing individual frames.
{
        int j,k;
        for(j=0;j<=len;j++)
        {
                printf("\n\t%d \t ",s1[j].seq_no);
                for(k=0;k<10;k++)
                        printf("%d",s1[j].frame[k]);
                printf("\t %d ",s1[j].parity);
                printf("\n");
        }
}
void error(int *a)  // To give the error to frame.
{
        int j;
        for(j=0;j<9;j++)
        {
                a[j]=a[j+1];
        }
}
 


void reciver(struct s s2[])
{
        int k,j,flg=1;
        error(&s2[0].frame[0]);  
        parityCount(&s2[0]);   
        printf("\n\n");               
        for(j=0;j<=len;j++)
        {
                if(s2[j].parity != tmp[j].parity)
                {
                        flg=0;
                        break;
                }
                else
                        flg=1;
        }
        printf("\n\n");
        if(flg==0)

                printf("\n Wrong Data Recievd..\n");
        else

                printf("\n Correct Data Recievd..\n");

        printf("\n Recived data..\n\n");
        print(s2);
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes