• 进程间通信——— 命名管道


    #include "NamedPipeServer.h"
    #include <iostream>
    
    
    using namespace std;
    
    CNamedPipeServer::CNamedPipeServer()
    {
    }
    
    
    CNamedPipeServer::~CNamedPipeServer()
    {
    }
    
    
    void CNamedPipeServer::CreateNamedPipeInServer()
    {
        HANDLE                    hEvent;
        OVERLAPPED                ovlpd;
    
        //首先需要创建命名管道
        //这里创建的是双向模式且使用重叠模式的命名管道
        hNamedPipe = CreateNamedPipeA(pPipeName,
            PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
            0, 1, 1024, 1024, 0, NULL);
        cout << "创建命名管道成功 ..." << endl << endl;
        if (INVALID_HANDLE_VALUE == hNamedPipe)
        {
            hNamedPipe = NULL;
            cout << "创建命名管道失败 ..." << endl << endl;
            return;
        }
    
        //添加事件以等待客户端连接命名管道
        //该事件为手动重置事件,且初始化状态为无信号状态
        hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
        if (!hEvent)
        {
            cout << "创建事件失败 ..." << endl << endl;
            return;
        }
    
        memset(&ovlpd, 0, sizeof(OVERLAPPED));
    
        //将手动重置事件传递给 ovlap 参数
        ovlpd.hEvent = hEvent;
    
        //等待客户端连接
        if (!ConnectNamedPipe(hNamedPipe, &ovlpd))
        {
            if (ERROR_IO_PENDING != GetLastError())
            {
                CloseHandle(hNamedPipe);
                CloseHandle(hEvent);
    
                cout << "等待客户端连接失败 ..." << endl << endl;
                return;
            }
        }
    
        //等待事件 hEvent 失败
        if (WAIT_FAILED == WaitForSingleObject(hEvent, INFINITE))
        {
            CloseHandle(hNamedPipe);
            CloseHandle(hEvent);
    
            cout << "等待对象失败 ..." << endl << endl;
            return;
        }
    
        CloseHandle(hEvent);
    }
    
    
    void CNamedPipeServer::NamedPipeReadInServer()
    {
        char *            pReadBuf;
        DWORD            dwRead;
    
        pReadBuf = new char[strlen(pStr) + 1];
        memset(pReadBuf, 0, strlen(pStr) + 1);
    
        //从命名管道中读取数据
        if (!ReadFile(hNamedPipe, pReadBuf, strlen(pStr), &dwRead, NULL))
        {
            delete[]pReadBuf;
    
            cout << "读取数据失败 ..." << endl << endl;
            return;
        }
        cout << "读取数据成功:    " << pReadBuf << endl << endl;
    }
    
    
    void CNamedPipeServer::NamedPipeWriteInServer(char* szBuffer)
    {
        DWORD            dwWrite;
    
        //向命名管道中写入数据
        if (!WriteFile(hNamedPipe, szBuffer, strlen(szBuffer), &dwWrite, NULL))
        {
            cout << "写入数据失败 ..." << endl << endl;
            return;
        }
        cout << "写入数据成功:    " << szBuffer << endl << endl;
    }
    #pragma once
    #pragma once
    
    #include <Windows.h>
    class CNamedPipeServer
    {
    public:
        CNamedPipeServer();
        ~CNamedPipeServer();
    
        HANDLE            hNamedPipe;
        const char *    pStr = "yifi";
        const char *    pPipeName = "\\.\pipe\yifiPipe";
        void CNamedPipeServer::NamedPipeReadInServer();
        void CNamedPipeServer::NamedPipeWriteInServer(char* szBuffer);
        void CNamedPipeServer::CreateNamedPipeInServer();
    
    
    };
    #include "stdafx.h"
    #include "NamedPipeClient.h"
    #include <iostream>
    
    using namespace std;
    
    
    CNamedPipeClient::CNamedPipeClient()
    {
    }
    
    
    CNamedPipeClient::~CNamedPipeClient()
    {
    }
    
    
    void CNamedPipeClient::OpenNamedPipeInClient()
    {
        //等待连接命名管道
        WaitNamedPipeA(pPipeName, NMPWAIT_WAIT_FOREVER);
        int a = GetLastError();
      /*  {
            cout<<"命名管道实例不存在 ..."<<endl<<endl;
            return;
        }*/
     
        //打开命名管道
        hNamedPipe = CreateFileA(pPipeName, GENERIC_READ | GENERIC_WRITE, 
            0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        if(INVALID_HANDLE_VALUE == hNamedPipe)
        {
            cout<<"打开命名管道失败 ..."<<endl<<endl;
            return;
        }
    }
     
     
    void CNamedPipeClient::NamedPipeReadInClient()
    {
        char                pReadBuf[1000];
        DWORD                dwRead;
        char                BufferData[1000];
        //从命名管道中读取数据
        if(!ReadFile(hNamedPipe, pReadBuf,4  , &dwRead, NULL))
        {
            delete []pReadBuf;
     
            cout<<"读取数据失败 ..."<<endl<<endl;
            return;
        }
    }
     
     
    void CNamedPipeClient::NamedPipeWriteInClient(char* BufferData)
    {
        DWORD                dwWrite;
     
        //向命名管道中写入数据
        if(!WriteFile(hNamedPipe, BufferData, strlen(BufferData), &dwWrite, NULL))
        {
            cout<<"写入数据失败 ..."<<endl<<endl;
            return;
        }
        cout<<"写入数据成功:    "<< BufferData <<endl<<endl;
    }
    #pragma once
    #include <Windows.h>
    class CNamedPipeClient
    {
    public:
        CNamedPipeClient();
        ~CNamedPipeClient();
    
    
        HANDLE            hNamedPipe;
    
         
        const char * pPipeName = "\\.\pipe\yifiPipe";
        //打开命名管道
        void OpenNamedPipeInClient();
    
        //客户端从命名管道中读取数据
        void NamedPipeReadInClient();
    
        //客户端往命名管道中写入数据
        void NamedPipeWriteInClient(char* BufferData);
    };
  • 相关阅读:
    Q:简单实现URL只能页面跳转,禁止直接访问
    Q:elementUI中tree组件动态展开
    一个切图仔的 JS 笔记
    一个切图仔的HTML笔记
    一个切图仔的 CSS 笔记
    GnuPG使用笔记
    SQL Svr 2012 Enterprise/Always-on节点连接超时导致节点重启——case分享
    网卡配置文件备份在原目录下引起网络配置异常
    python培训
    service脚本的写法
  • 原文地址:https://www.cnblogs.com/yifi/p/6596584.html
Copyright © 2020-2023  润新知