有名管道实现简单的聊天功能

实现效果是对方有回答了才能够继续下一步

chatA.c

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>

int main(){

    //1.判断管道文件是否存在
    int ret = access("fifo1", F_OK);
    if(ret == -1){
        //文件不存在
        printf("管道文件不存在,创建管道文件\n");
        ret = mkfifo("fifo1",0664);
        if(ret == -1){
            perror("mkfifo");
            exit(0);
        }
    }
    
    ret = access("fifo2", F_OK);
    if(ret == -1){
        //文件不存在
        printf("管道文件不存在,创建管道文件\n");
        ret = mkfifo("fifo2",0664);
        if(ret == -1){
            perror("mkfifo");
            exit(0);
        }
    }

    //2. 以只写的方式打开管道fifo1
    int fdw = open("fifo1", O_WRONLY);    

    if(fdw == -1){
        perror("open");
        exit(0);
    }
    printf("打开fifo1成功,等待写入数据\n");

    //3. 以只读的方式打开管道fifo2
    int fdr = open("fifo2", O_RDONLY);    

    if(fdr == -1){
        perror("open");
        exit(0);
    }
    printf("打开fifo2成功,等待读取数据\n");

    char buf[128];

    //循环的写读数据
    while(1){
        memset(buf, 0, 128);
        //获取标准输入的数据
        fgets(buf, 128, stdin);
        //写数据
        ret = write(fdw, buf, strlen(buf));
        if(ret == -1){
            perror("write");
            exit(0);
        }
        //读管道数据
        memset(buf, 0, 128);
        ret = read(fdr, buf, 128);
        if(ret <= 0){
            perror("read");
            exit(0);
        }
        printf("buf : %s\n", buf);

    }

    close(fdr);
    close(fdw);
    return 0;
}

chatB.c

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>

int main(){

    //1.判断管道文件是否存在
    int ret = access("fifo1", F_OK);
    if(ret == -1){
        //文件不存在
        printf("管道文件不存在,创建管道文件\n");
        ret = mkfifo("fifo1",0664);
        if(ret == -1){
            perror("mkfifo");
            exit(0);
        }
    }
    
    ret = access("fifo2", F_OK);
    if(ret == -1){
        //文件不存在
        printf("管道文件不存在,创建管道文件\n");
        ret = mkfifo("fifo2",0664);
        if(ret == -1){
            perror("mkfifo");
            exit(0);
        }
    }

    //2. 以只读的方式打开管道fifo1
    int fdr = open("fifo1", O_RDONLY);    

    if(fdr == -1){
        perror("open");
        exit(0);
    }
    printf("打开fifo1成功,等待读取数据\n");

    //3. 以只写的方式打开管道fifo2
    int fdw = open("fifo2", O_WRONLY);    

    if(fdw == -1){
        perror("open");
        exit(0);
    }
    printf("打开fifo2成功,等待写入数据\n");

    char buf[128];

    //循环的读写数据
    while(1){
        //读管道数据
        memset(buf, 0, 128);
        ret = read(fdr, buf, 128);
        if(ret <= 0){
            perror("read");
            exit(0);
        }
        printf("buf : %s\n", buf);

        memset(buf, 0, 128);
        //获取标准输入的数据
        fgets(buf, 128, stdin);
        //写数据
        ret = write(fdw, buf, strlen(buf));
        if(ret == -1){
            perror("write");
            exit(0);
        }
        

    }

    close(fdr);
    close(fdw);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值