Tuesday, 13 August 2013

Why the FIFOs server cannot receive the data from client? use named-pipes

Why the FIFOs server cannot receive the data from client? use named-pipes

I just do a experiment of named-pipes in Ubuntu,but the server cannot
receive the string which I input in the client,how can I solve it ? when I
press Ctrl+C in the client,the sever can receive some characters.
Server
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<unistd.h>
#include<linux/stat.h>
#define FIFO_FILE "sampleFIFO"
int main()
{
FILE *fp;
char readbuf[80];
umask(0);
mknod(FIFO_FILE,S_IFIFO|0666,0);
while(1)
{
fp = fopen(FIFO_FILE,"r");
fgets(readbuf,80,fp);
printf("Received:%s\n",readbuf);
fclose(fp);
}
return(0);
}
Client
#include<stdio.h>
#include<stdlib.h>
#define FIFO_FILE "sampleFIFO"
int main(int argc,char *argv[])
{
FILE *fp;
char buf[80];
if((fp = fopen(FIFO_FILE,"w")) == NULL)
{
perror("error");
exit(1);
}
while(1)
{
memset(buf,0,sizeof(buf));
fgets(buf,80,stdin);
fputs(buf,fp);
}
fclose(fp);
return(0);
}

No comments:

Post a Comment