linux有名管道简单聊天实现

1.习惯用命令创建两个管道 mkfifo fifo1 fifo2

在这里插入图片描述
在这里插入图片描述

用户A

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

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

#define SIZE 64
//先读后写
//以只读方式打开管道1
//以只写方式打开管道2
int main(void)
{
        int fdr=-1;
        int fdw=-1;
        int ret=-1;
        char buf[SIZE];
        //只读方式打开管道1
        fdr=open("fifo1",O_RDONLY);
        if(-1==fdr){
                perror("open");
                return 1;
        }
        printf("以只读方式打开管道1 ok...\n");

        fdw=open("fifo2",O_WRONLY);
        if(-1==fdw){
                perror("open");
                return 1;
        }
        printf("以只写方式打开管道2 ok.....\n");

        while(1){

                //读管道1
                memset(buf,0,SIZE);
                ret=read(fdr,buf,SIZE);
                if(ret<=0){
                        perror("read");
                        break;
                }
                printf("read: %s\n",buf);

                //写管道2
                memset(buf,0,SIZE);
                fgets(buf,SIZE,stdin);
                //去除最后一个换行符
                if('\n'==buf[strlen(buf)-1]){
                        buf[strlen(buf)-1]='\0';
                }

                ret=write(fdw,buf,strlen(buf));
                if(ret<=0){
                        perror("write");
                        break;
                }
                printf("write ret :%d\n",ret);
        }

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

用户B

在这里插入代码片#include<stdio.h>
#include<string.h>
#include<stdlib.h>

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

#define SIZE 64
//先写后读
//以只写方式打开管道1
//以只读方式打开管道2
int main(void)
{
        int fdr=-1;
        int fdw=-1;
        int ret=-1;
        char buf[SIZE];

        fdw=open("fifo1",O_WRONLY);
        if(-1==fdw){
                perror("open");
                return 1;
        }
        printf("以只写方式打开管道1 ok.....\n");

        //只读方式打开管道2
        fdr=open("fifo2",O_RDONLY);
        if(-1==fdr){
                perror("open");
                return 1;
        }
        printf("以只读方式打开管道2 ok.....\n");
        while(1){


                //写管道1
                memset(buf,0,SIZE);
                fgets(buf,SIZE,stdin);
                //去除最后一个换行符
                if('\n'==buf[strlen(buf)-1]){
                        buf[strlen(buf)-1]='\0';
                }

                ret=write(fdw,buf,strlen(buf));
                if(ret<=0){
                        perror("write");
                        break;
                }
                printf("write ret :%d\n",ret);
                //读管道2
                memset(buf,0,SIZE);
                ret=read(fdr,buf,SIZE);
                if(ret<=0){
                        perror("read");
                        break;
                }
                printf("read: %s\n",buf);

        }

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

细心的伙伴们肯定发现,两者代码只是顺序不同,代码其实一模一样,所以使用有名管道只需要记住调用的管道读写顺序即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值