[C/C++ realizes inter-process communication 3] Pipeline communication mechanism

Previous situation review

The previous issue has explained the concept of process and the implementation principle of inter-process communication. The following only shows the relevant code of the pipeline communication mechanism to realize inter-process communication.

train of thought

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

source code

Environment: Windows 64-bit + QtCreator
involves some functions in WINAPI. For specific function analysis, please refer to the official WINAPI document

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;
}

Note: The running sequence of the above programs should be: start Publisher.exe first, and then start Subscriber.exe.

Effect

insert image description here

Guess you like

Origin blog.csdn.net/wddkxg/article/details/131487033