Linux:命名管道实现IPC(进程间通信七)

上篇博客我们说过在这篇博客我们将改进我们上篇博客写的,命名管道实现简易通信,下面是改进之后的代码,这里我不详细写出来,希望大家可以凭借上节课的知识以及代码内容,真正的自己理解并且复现一遍~~

Makefile:

cpp 复制代码
.PHONEY : all
all : server client
server : server.cc
	g++ server.cc -o server
client : client.cc
	g++ client.cc -o client

.PHONEY : clean
clean :
	rm -f client server

server.cc:

cpp 复制代码
#include "common.hpp"

int main()
{
    NamedFifo fifo(PATH, FIFO_FILE);
    FifoOper server(PATH, FIFO_FILE);
    server.OpenForRead();
    server.Read();
    server.Close();
    return 0;
}

client.cc:

cpp 复制代码
#include "common.hpp"

int main()
{
    FifoOper client(PATH, FIFO_FILE);
    client.OpenForWrite();
    client.Write();
    client.Close();
    return 0;
}

common.hpp:

cpp 复制代码
#pragma once

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

#define PATH "."
#define FIFO_FILE "fifo"

class NamedFifo
{
public:
    NamedFifo(std::string path, std::string name)
        : _path(path)
        , _name(name)
    {
        umask(0);
        _fifoname = _path + "/" + _name;
        int n = mkfifo(_fifoname.c_str(), 0666);
        if (n == -1)
        {
            std::cout << "mkfifo fail" << std::endl;
        }
        else 
        {
            std::cout << "mkfifo success" << std::endl;
        }
    }

    ~NamedFifo()
    {
        int n = unlink(_fifoname.c_str());
        if (n == -1)
        {
            std::cout << "~fifo fail" << std::endl;
        }
        else 
        {
            std::cout << "~fifo success" << std::endl;
        }
    }
private:
    std::string _path;
    std::string _name;
    std::string _fifoname;
};

class FifoOper
{
public:
    FifoOper(std::string path, std::string name)
        : _path(path)
        , _name(name)
        , _fd(-1)
    {
        _fifoname = _path + "/" + _name;
    }

    ~FifoOper()
    {
        std::cout << "~FifoOper" << std::endl;
    }

    void OpenForRead()
    {
        _fd = open(_fifoname.c_str(), O_RDONLY);
        if (_fd < 0)
        {
            std::cout << "OpenForRead fail" << std::endl;
            return;
        }
        std::cout << "OpenForRead success" << std::endl;
    }

    void OpenForWrite()
    {
        _fd = open(_fifoname.c_str(), O_WRONLY);
        if (_fd < 0)
        {
            std::cout << "OpenForWrite fail" << std::endl;
            return;
        }
        std::cout << "OpenForWrite success" << std::endl;
    }

    void Read()
    {
        char buffer[1024];
        while (true)
        {
            int number = read(_fd, buffer, sizeof(buffer) - 1);
            if (number > 0)
            {
                buffer[number] = '\0';
                std::cout << "client say : " << buffer << std::endl;
            }
            else if (number == 0)
            {
                std::cout << "client quit! server quit, too!" << std::endl;
                break;
            }
            else 
            {
                std::cout << "Read error" << std::endl;
                break;
            }
        }
    }

    void Write()
    {
        std::string message;
        while (true)
        {
            std::cout << "please enter# ";
            std::getline(std::cin, message);
            write(_fd, message.c_str(), message.size());
        }
    }

    void Close()
    {
        if (_fd > 0)
            close(_fd);
    }
private:
    std::string _path;
    std::string _name;
    std::string _fifoname;
    int _fd;
};

这就算有关管道通信的知识啦(命名管道与匿名管道),但是我们的通信之旅还没有完,大家敬请期待后续内容啦~~

相关推荐
天蓝蓝的本我9 小时前
linux本地部署Qwen3.8 27B
linux·运维·excel
n112129 小时前
服务器被反复尝试登录之后:fail2ban 加密钥登录的五道加固
运维·服务器·服务器安全·fail2ban·ssh加固
洋不写bug9 小时前
网络编程(二)TCP回显服务器与客户端通信详解
服务器·网络·tcp/ip·tcp·网络通信·javaee·回显服务器
java_logo9 小时前
Docker 部署 DeepSeek Harness:轻松搭建局域网里的 AI Agent 平台
运维·docker·容器·ai agent·deepseek·轩辕镜像·deepseekharness
lisanmengmeng9 小时前
NRPE 添加命令(四)
linux·运维·服务器
想做小南娘,发现自己是女生喵9 小时前
i.MX6ULL嵌入式Linux入门学习文档
linux·运维·学习
FL162386312910 小时前
Siemens‑Automation‑License‑Manager & SIMATIC‑Automation‑Tool 下载安装教程
运维·服务器
落魄大学生之流水线上谋生计10 小时前
从 Jenkins 小白到企业级交付平台
运维·jenkins
此生决int10 小时前
Linux系列(04)——基础开发工具(上)软件包管理器、vim、gcc/g++
linux·服务器·vim
Huangjin007_10 小时前
【Linux 系统篇(二十六)】文件(三): 深入理解 “一切皆文件”、缓冲区
linux·运维·服务器