IPC中用FIFO进行客户与服务器传送文件的问题
//client.c#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "unpipc.h"
#include "my_err.h"
#define SERV_FIFO "/work/test/test.serv"
int main()
{
char fifoname[MAXLINE],buff[MAXLINE];
//read pathname
fgets(fifoname,MAXLINE,stdin);
size_t len=strlen(fifoname);
fifoname[len-1]='\0';
//create FIFO whith pathname
pid_t pid=getpid();
sprintf(fifoname,"%s:%ld",fifoname,pid);
if((mkfifo(fifoname,FILE_MODE)<0)&&(errno!=EEXIST))
err_sys("can't create %s",fifoname);
//open FIFO to server and write pathname to FIFO
int writefifo=open(SERV_FIFO,O_WRONLY,0);
write(writefifo,fifoname,len);
//now open our FIFO;blocks until server opens for writing
int readfifo=open(fifoname,O_RDONLY,0);
//read from IPC,write to standard output
ssize_t n;
while((n=read(readfifo,buff,MAXLINE))>0)
write(STDOUT_FILENO,buff,n);
close(readfifo);
unlink(fifoname);
exit(0);
}
//server.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "my_err.h"
#include "unpipc.h"
#define SERV_FIFO "/work/test/test.serv"
int main()
{
char *ptr=NULL,buff[MAXLINE+1],fifoname[MAXLINE];
if((mkfifo(SERV_FIFO,FILE_MODE)<0)&&(errno!=EEXIST))
err_sys("can't create %s",SERV_FIFO);
//open sever's well-know FIFO reading and write
int readfifo=open(SERV_FIFO,O_RDONLY,0);
int dummyfd=open(SERV_FIFO,O_WRONLY,0);
//read pathname from IPC(is from client pathname) to fifoname
ssize_t n;
if((n=read(readfifo,fifoname,MAXLINE))==0)
err_quit("end of file while reading pathname");
if((ptr=strchr(fifoname,':'))==NULL)
{
err_msg("bogus resquest: %s",fifoname);
}
*ptr++=0;
//open write text to pathname(fifoname)
int writefd=open(fifoname,O_WRONLY,0);
//write text(from SERV_FIFO) to pathname(fifoname)
int fd;
if((fd=open(ptr,O_RDONLY))<0)
{
int len=strlen(fifoname);
write(writefd,fifoname,len);
close(writefd);
}
else
{
while((n=read(fd,buff,MAXLINE))>0)
write(writefd,buff,n);
close(fd);
close(writefd);
}
exit(0);
}
代码描述:
用一个众所周知的路径名创建一个服务器FIFO,然后客户机上用客户输入的路径名+pid创建一个客户FIFO,
客户把想要读取文件的路径名通过服务器的FIFO传送给服务器,服务器找到文件传送给客户,输出到屏幕上。
问题,屏幕上总是不显示文件内容