【C/C++实现进程间通信 三】管道通信机制

前情回顾

上一期已经讲解过了进程的相关概念以及进程间通信的实现原理,下面仅展示管道通信机制实现进程间通信的相关代码。

思路

/*
    本项目主要用于以管道通信的方式进行进程间通信的测试。
    1.主要包含PublisherDemo和SubscriberDemo两个子项目,分别用于发送信息和接收信息。
    2.在PublisherDemo中,程序创建一个命名管道,并连接到该管道。然后,使用WriteFile函数向管道写入消息。
    3.在SubscriberDemo中,程序连接到同一个命名管道,并使用ReadFile函数从管道读取消息,并打印收到的消息。
*/

源码

环境:Windows 64位 + QtCreator
涉及到一些WINAPI中的函数,具体函数解析可自行参考WINAPI官方文档

Publisher.cpp

#include <iostream>
#include <Windows.h>
#include <WinUser.h>

#pragma comment "win32.lib"

int main() {
    
    
    HANDLE hPipe;
    char buffer[1024];
    DWORD bytesRead;

    // 创建管道
    hPipe = CreateNamedPipe(
        L"\\\\.\\pipe\\MyPipe",     // 管道名称
        PIPE_ACCESS_OUTBOUND,      // 只用于写入
        PIPE_TYPE_BYTE | PIPE_WAIT,// 字节类型,阻塞模式
        1,                         // 最多可以有一个实例
        0,                         // 输出缓冲区大小
        0,                         // 输入缓冲区大小
        0,                         // 默认超时时间
        NULL                       // 默认安全性
    );

    if (hPipe == INVALID_HANDLE_VALUE) {
    
    
        std::cout << "Error: Failed to create pipe!" << std::endl;
        return 1;
    }
    std::cout << "Success to Create MyPipe!" << std::endl;

    // 连接到管道
    if (!ConnectNamedPipe(hPipe, NULL)) {
    
    
        std::cout << "Error: Failed to connect to pipe!" << std::endl;
        CloseHandle(hPipe);
        return 1;
    }
    std::cout << "Success to connect MyPipe!" << std::endl;

    // 发送消息
    strcpy_s(buffer, "Hello, cxk!");
    while(1)
    {
    
    
        if (!WriteFile(hPipe, buffer, strlen(buffer) + 1, &bytesRead, NULL)) {
    
    
            std::cout << "Error: Failed to write to pipe!" << std::endl;
            CloseHandle(hPipe);
            return 1;
        }
        std::cout << "Send message: Hello, cxk!" << std::endl;
        Sleep(1000);
    }


    // 关闭管道
    CloseHandle(hPipe);
    getchar();
    return 0;
}

Subscriber.cpp

#include <iostream>
#include <Windows.h>

int main() {
    
    
    HANDLE hPipe;
    char buffer[1024];
    DWORD bytesRead;

    // 连接到管道
    hPipe = CreateFile(
        L"\\\\.\\pipe\\MyPipe", // 管道名称
        GENERIC_READ,          // 只用于读取
        0,                     // 共享模式,默认为独占
        NULL,                  // 默认安全性
        OPEN_EXISTING,         // 打开现有的管道
        0,                     // 默认属性
        NULL                   // 默认模板
    );

    if (hPipe == INVALID_HANDLE_VALUE) {
    
    
        std::cout << "Error: Failed to connect to pipe!" << std::endl;
        return 1;
    }
    std::cout << "Success to connect MyPipe!" << std::endl;

    while(1)
    {
    
    
        // 读取消息
        if (!ReadFile(hPipe, buffer, sizeof(buffer), &bytesRead, NULL)) {
    
    
            std::cout << "Error: Failed to read from pipe!" << std::endl;
            CloseHandle(hPipe);
            return 1;
        }

        std::cout << "Received message: " << buffer << std::endl;

        Sleep(1000);
    }

    // 关闭管道
    CloseHandle(hPipe);

    return 0;
}

注:以上程序运行顺序应该为:先启动Publisher.exe,再启动Subscriber.exe。

效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wddkxg/article/details/131487033