【Linux】有名管道实现简单版聊天功能

橙色

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

创建两个.c文件,一个是chata.c,一个是chatb.c

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");
	
	//之所以需要两个进程,是因为在一个进程中,由于while死循环,该进程向一个有名管道中写入内容后,
	//就必须在另一个管道读取一次内容,如果写入而没有读取(也就是对方写入)的话,就会阻塞在读取这里
	//呈现的效果就是a向b先发送多条消息,但b仅能接收到一条,在b回复后,a像b发送的除第一条消息外剩下的消息才会才发送过来
	//所以需要两个进程分别执行在两个有名管道中读和写的操作。
    pid_t pid = fork();

    if(pid>0){
        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);
        }       
    }
    close(fdw);
    }

    else{
        char buf[128];
        while(1) {
        memset(buf, 0, 128);
        
        // 5.读管道数据
        memset(buf, 0, 128);
        ret = read(fdr, buf, 128);
        if(ret <= 0) {
            perror("read");
            break;
        }
        printf("buf: %s\n", buf);
    }
    // 6.关闭文件描述符
    close(fdr);
    }
    
    // 6.关闭文件描述符
    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() {

    int ret=access("fifo1",F_OK);
    if(ret==-1){
        printf("管道不存在,创建对应的有名管道\n");
        ret=mkfifo("fifo1",0664);
        if(ret==-1){
            perror("fifo1");
            exit(0);
        }  
    }
    ret=access("fifo2",F_OK);
    if(ret==-1){
        printf("管道不存在,创建对应的有名管道\n");
        ret=mkfifo("fifo2",0664);
        if(ret==-1){
        perror("fifo2");
        exit(0);
        }
    }

    int fdr=open("fifo1",O_RDONLY);
    if(fdr==-1){
        perror("open");
        exit(0);
    }
    printf("打开管道fifo1成功,等待读取...\n");
    int fdw=open("fifo2",O_WRONLY);
    if(fdw==-1){
        perror("open");
        exit(0);
    }
    printf("打开管道fifo2成功,等待写入...\n");

    pid_t pid = fork();

    if(pid>0){
        char buf[128];
        while (1){
            memset(buf,0,128);
            ret=read(fdr,buf,sizeof(buf));
            if(ret<=0){
                perror("read");
                break;
            }
        printf("buf:%s\n",buf);
        } 
    close(fdr);
    }

    else{
        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);
            }
        } 
        close(fdw);
    }

    return 0;
}

在xshell中编译两个文件得到两个可执行文件chata和chatb,接着在xshell的两个窗口分别执行chata和chatb两个可执行程序,即可实现对话。
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力学习的小马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值