我只是在Ubuntu中做了一个命名管道的实验,但是服务器无法接收我在客户端输入的字符串,我该如何解决?当我在客户端按 Ctrl+C 时,服务器可以接收一些字符。
服务器
#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);
}
客户
#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);
}