将多个客户端fifo连接到一台服务器fifo

必须编写两个程序(一个客户端和一个服务器)来完成 使用FIFO彼此聊天(将消息从一个进程传递到另一个进程)。的 服务器进程创建一个SERVER_FIFO仅接收客户端连接。服务器 维护在线客户列表。每个客户端创建自己的CLIENT_FIFO以接收 来自服务器的命令,将使用system()系统调用在客户端执行。您可以使用 getpid()系统调用,以检索要在以下位置连接的客户端的进程ID CLIENT_FIFO名称。

我只创建两个可以互相通信的fifo

服务器

// C program to implement one side of FIFO 
// This side writes first,then reads 
#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"; 

    mkfifo(myfifo,0666); 

    char arr1[80],arr2[80]; 
    while (1) 
    { 

        fd = open(myfifo,O_WRONLY); 

        fgets(arr2,80,stdin); 

        write(fd,arr2,strlen(arr2)+1); 
        close(fd); 

        // Open FIFO for Read only 
        fd = open(myfifo,O_RDONLY); 

        // Read from FIFO 
        read(fd,arr1,sizeof(arr1)); 

        // Print the read message 
        printf("User2: %s\n",arr1); 
        close(fd); 
    } 
    return 0; 
} 

================================================ ==================

客户

// C program to implement one side of FIFO 
// This side reads first,then reads 
#include <stdio.h> 
#include <string.h> 
#include <fcntl.h> 
#include <sys/stat.h> 
#include <sys/types.h> 
#include <unistd.h> 

int main() 
{ 
    int fd1; 

    // FIFO file path 
    char * myfifo = "/tmp/myfifo"; 

    mkfifo(myfifo,0666); 

    char str1[80],str2[80]; 
    while (1) 
    { 
        // First open in read only and read 
        fd1 = open(myfifo,O_RDONLY); 
        read(fd1,str1,80); 

        // Print the read string and close 
        printf("User1: %s\n",str1); 
        close(fd1); 


        fd1 = open(myfifo,O_WRONLY); 
        fgets(str2,stdin); 
        write(fd1,str2,strlen(str2)+1); 
        close(fd1); 
    } 
    return 0; 
} 
napkinnn 回答:将多个客户端fifo连接到一台服务器fifo

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3119749.html

大家都在问