postheadericon Byte Stuffing - Framing Technics


Sender



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


#define FLAG '#'

#define ESC '*'

void main()
{
int j=1,i,pid;

char Frame_Data[50],Targeted_Frame[50];

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

printf("Enter the Frame Data : ");
  scanf("%s",Frame_Data);

Targeted_Frame[0]=FLAG; //set header FLAG

for(i=0;i<strlen(Frame_Data);i++)
{
    if (Frame_Data[i]==FLAG || Frame_Data[i]==ESC) //check FLAG or ESCAPE charechter is in frame data
{
    Targeted_Frame[j++]=ESC; // if FLAG / ESCAPE Char found add one more ESCAPE char

}
    Targeted_Frame[j++]=Frame_Data[i]; //copy other data

  }
Targeted_Frame[j++]=FLAG; //set tailer FLAG
Targeted_Frame[j]='\0';

printf("\nFrame to be send  : %s",Targeted_Frame);
write(pid,&Targeted_Frame,sizeof(Targeted_Frame));
close(pid);
}



Receiver


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

#define FLAG '#'

#define ESC '*'

void main()
{
int j=0,i=1,pid;
char str[50],targeted_frame[50],frame_data[50];

pid=open("pipe",O_RDONLY);
read(pid,&targeted_frame,sizeof(targeted_frame));

printf("\ndata received is %s",targeted_frame);

while(targeted_frame[i+1]!='\0')
{
    if (targeted_frame[i]==ESC) //check ESCAPE char is in DATA?
{
i++; //remove ESCAPE charecter
}
frame_data[j]=targeted_frame[i];
i++;
j++;
  }
frame_data[j]='\0';
printf("\nframe received : %s\n",frame_data);
close(pid);
}


0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.