Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Tuesday 12 June 2012

BYTE STUFFING


[ SENDER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#define flag '*'
#define esc '#'
void main()
{
int j=1,i,pid;
char str[50],frame[50];
system(">pipe");
pid=open("pipe",O_WRONLY);
printf("Enter the string:");
  scanf("%s",str);
frame[0]=flag;
  for(i=0;i<strlen(str);i++)
{
    if (str[i]==flag || str[i]==esc)
{
    frame[j]=esc;
    j++;
}
    frame[j]=str[i];
j++;
  }
frame[j++]=flag;
frame[j]='\0';
printf("\nframe to send  : %s",frame);
write(pid,&frame,sizeof(frame));
close(pid);
}

[ RECEIVER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#define flag '*'
#define esc '#'
void main()
{
int j=0,i=1,pid;
char str[50],frame[50],data[50];

pid=open("pipe",O_RDONLY);
read(pid,&frame,sizeof(frame));
printf("\ndata received is %s",frame);
while(frame[i+1]!='\0')
{
    if (frame[i]==esc)
{
i++;
}
data[j]=frame[i];
i++;
j++;
  }
data[j]='\0';
printf("\nactual data is %s",data);
close(pid);
}

1 comment:

  1. what to do using filehandling this program

    ReplyDelete