【发布时间】:2019-01-22 13:54:01
【问题描述】:
我正在尝试编写一个客户端-服务器程序,其中有三个可执行文件 D1、D2 和 D3,它们提供一些数据作为输出。客户端请求这些数据源中的任何一个,并在通用 fifo 的帮助下将其 pid 发送到服务器。发送这个请求的结构是:
struct Request
{
char p[10]; // the pid of the client program in string form
int req; // 1,2,or 3 depending on which one is required D1,D2 or D3
};
服务器收到请求后会打开一个fifo,其路径名是客户端的pid。所以它可以作为客户端特定的先进先出。
mkfifo(pid,O_CREAT|0666);
int fd1 = open(pid,O_WRONLY);
现在,假设 req 字段为 1。如果是 D1 的第一个请求,Server 将运行:
FILE* fp = popen("./D1","r");
int fd = fileno(fp); //for getting the file descriptor for the reading end of the pipe connected to D1
现在我希望我的客户从 D1.D1 的管道中读取包含简单的逻辑程序,例如:
while(1)
{
write(1,"Data from D1",12);
sleep(1);
}
我试过 dup2(fd,fd1) 但没有用。有什么方法可以连接两个文件描述符 fd 和 fd1?
另外,如果另一个客户端请求D1,如何将client2的文件描述符连接到fd,以便两个客户端一起接收相同的消息?
【问题讨论】:
标签: unix pipe client-server fifo