我有一个任务,必须使用fifo将文件从客户端进程传输到服务器进程。我试图将fifo文件作为我们在系统中创建的其他文件进行处理。它编译时没有任何错误,但是没有正确执行。有人可以给我一个关于计算机系统内部的fifo文件结构的想法吗?到目前为止,我知道如何使用fifo文件使用create(),read(),write(),open()函数。此外,如果有人可以帮助我更正,我将不胜感激。我的程序?
我的客户端和服务器程序如下:-
客户端程序:-
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int fd;
char *myfifo ="/tmp/myfifo";
char str[80];
FILE *fp;
char filename[20];
printf("\nEnter filename: ");
gets(filename);
mkfifo(myfifo,0666);
fp = fopen(filename,"r");
if(fp == NULL)
{
printf("\nError opening the file");
exit(1);
}
fd = open(myfifo,O_WRONLY);
while(fgets(str,80,fp)!=NULL)
{
write(fd,str,strlen(str)+1);
}
close(fd);
fclose(fp);
return 0;
}
客户端程序:-
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int fd1;
char *myfifo ="/tmp/myfifo";
char str1[80],filename[20];
FILE *fp1,*fp2;
fd1= open(myfifo,O_RDONLY);
fp1 = fopen(filename,"r");
fp2 = fopen(filename,"w");
while(!feof(fp1))
{
read(fd1,str1,strlen(str1)+1);
fputs(str1,fp2);
}
return 0;
}