有名管道实现聊天功能(一问一答)

一、逻辑实现

在这里插入图片描述

  1. 有两个无关的进程A和B;
  2. 在进程A或B中创建有名管道1和2;
  3. 在进程A中,以只写方式打开有名管道1,以只读方式打开有名管道2,在while循环里,用gets()获取键盘输入,将输入写管道1,然后读管道2;
  4. 在进程B中,以只读方式打开有名管道1,以只写方式打开有名管道2,在while循环里,读管道1,然后用gets()获取键盘输入,将输入写管道2;
  5. 完成通信。

二、代码实现

  • A.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>


int main(int argc, char const *argv[])
{
    time_t timer;//time_t就是long int 类型
    struct tm *tblock;
    timer = time(NULL);



    //1.判断文件是否存在
    int ret = access("fifo1",F_OK);
    if(ret==-1)
    {
        printf("fifo1管道不存,创建管道!\n");
        //2.创建管道
        ret = mkfifo("fifo1",0664);
        if(ret==-1)
        {
            perror("mkfifo");
            exit(0);
        }
    }
    ret = access("fifo2",F_OK);
    if(ret==-1)
    {
        printf("fifo2管道不存,创建管道!\n");
        //2.创建管道
        ret = mkfifo("fifo2",0664);
        if(ret==-1)
        {
            perror("mkfifo");
            exit(0);
        }
    }
    //以只写的方式打开管道1
    int fd1 = open("fifo1",O_WRONLY);//以只写的方式打开管道
    if(fd1==-1)
    {
        perror("open");
        exit(0);
    }


    //以只读的方式打开管道2
    int fd2 = open("fifo2",O_RDONLY);
    if(fd2==-1)
    {
        perror("open");
        exit(0);
    }

    char buf[1024]={0};

    while(1)
    {
        fgets(buf,sizeof(buf),stdin);
        //写管道1
        timer = time(NULL);
        tblock = localtime(&timer);
        write(fd1,buf,sizeof(buf));
        printf("%sA say:%s\n",asctime(tblock),buf);
        memset(buf,0,sizeof(buf));


        //读管道2
        int len = read(fd2,buf,sizeof(buf));
        if(len==0)
        {
            printf("B断开连接!\n");
            exit(0);
        }
        else if(len>0)
        {
            timer = time(NULL);
            tblock = localtime(&timer);
            printf("%sB say:%s\n",asctime(tblock),buf);
            memset(buf,0,sizeof(buf));
        }
    }
    return 0;
}
  • B.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
//
int main(int argc, char const *argv[])
{
    time_t timer;//time_t就是long int 类型
    struct tm *tblock;


    //1.判断文件是否存在
    int ret = access("fifo1",F_OK);
    if(ret==-1)
    {
        printf("fifo1管道不存,创建管道!\n");
        //2.创建管道
        ret = mkfifo("fifo1",0664);
        if(ret==-1)
        {
            perror("mkfifo");
            exit(0);
        }
    }
    ret = access("fifo2",F_OK);
    if(ret==-1)
    {
        printf("fifo2管道不存,创建管道!\n");
        //2.创建管道
        ret = mkfifo("fifo2",0664);
        if(ret==-1)
        {
            perror("mkfifo");
            exit(0);
        }
    }
    //以只读的方式打开管道1
    int fd1 = open("fifo1",O_RDONLY);//以只写的方式打开管道
    if(fd1==-1)
    {
        perror("open");
        exit(0);
    }


    //以只写的方式打开管道2
    int fd2 = open("fifo2",O_WRONLY);
    if(fd2==-1)
    {
        perror("open");
        exit(0);
    }

    char buf[1024] = {0};
    while(1)
    {
        // 读管道1
        int len = read(fd1,buf,sizeof(buf));
        if(len==0)
        {
            printf("A断开连接\n");
            exit(0);
        }
        else if(len>0)
        {
            timer = time(NULL);
            tblock = localtime(&timer);
            printf("%sA say:%s\n",asctime(tblock),buf);
            memset(buf,0,sizeof(buf));
        }
        else if(len==-1)
        {
            perror("read");
        }

        //写管道2
        fgets(buf,sizeof(buf),stdin);
        timer = time(NULL);
        tblock = localtime(&timer);
        write(fd2,buf,sizeof(buf));
        printf("%sB say:%s\n",asctime(tblock),buf);  
        memset(buf,0,sizeof(buf));

    }  
    return 0;
}

三、效果展示

在这里插入图片描述

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值