send
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
int main(){
int pid,i,frmcnt=1;
char data[BUFSIZ]={'\0'},frame[BUFSIZ]={'\0'},length[BUFSIZ];
FILE *fp;
fp=fopen("send.txt","r");
if(!fp){perror("Message: ");return 0; }
pid=open("MyPipe",O_WRONLY);
system("clear");
do{
fgets(data,sizeof(data),fp);
if(feof(fp))
{break;}
printf("\nOriginal Data : %s ",data);
sprintf(frame,"%d#%s",(int)strlen(data),data);
write(pid,frame,sizeof(frame));
frmcnt++;
memset(data,0,sizeof(data));
memset(frame,0,sizeof(frame));
}
while(1);
unlink("MyPipe");
fclose(fp);
close(pid);
return 0;
}
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
int main(){
int pid,i,frmcnt=1;
char data[BUFSIZ]={'\0'},frame[BUFSIZ]={'\0'},length[BUFSIZ];
FILE *fp;
fp=fopen("send.txt","r");
if(!fp){perror("Message: ");return 0; }
pid=open("MyPipe",O_WRONLY);
system("clear");
do{
fgets(data,sizeof(data),fp);
if(feof(fp))
{break;}
printf("\nOriginal Data : %s ",data);
sprintf(frame,"%d#%s",(int)strlen(data),data);
write(pid,frame,sizeof(frame));
frmcnt++;
memset(data,0,sizeof(data));
memset(frame,0,sizeof(frame));
}
while(1);
unlink("MyPipe");
fclose(fp);
close(pid);
return 0;
}
rec.c
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
int main(){
int pid,i=0,len,cnt=0,frmcnt=1;
char frame[BUFSIZ],data[BUFSIZ]={'\0'},length[5]={'\0'};
int rc=mkfifo("MyPipe",0666);
FILE *fp=fopen("rec.txt","w");
//
pid=open("MyPipe",O_RDONLY);
system("clear");
if(!fp){perror("Message: ");return 0; }
while(read(pid,frame,sizeof(frame))){
printf("\nOriginal Data : %d :%s",frmcnt,frame);
do{
length[i]=frame[i];
i++;
}while(frame[i]!='#');
len=atoi(length);
i++;
len=len+i;
for(;i<len;i++){
data[cnt++]=frame[i];
}
data[cnt]='\0';
printf("%d message %s",frmcnt,data);
i=0;len=0;cnt=0;frmcnt++;
memset(data,0,sizeof(data));
memset(frame,0,sizeof(frame));
}
unlink("MyPipe");
fclose(fp);
close(pid);
return 0;
}
Post a Comment