Example of Windows Named Pipe Communication between Server and Client

在这里插入图片描述
服务端代码:

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    HANDLE hPipe = INVALID_HANDLE_VALUE;
    const char* lpszPipename = ("\\\\.\\pipe\\namedpipe_td");
    do
    {
        hPipe = CreateNamedPipeA(lpszPipename, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 1024, 1024, 0, NULL);
        if (hPipe == INVALID_HANDLE_VALUE)
        {
            cout << "Create Named pipe failed, error: " << hex << GetLastError() << endl;
            break;
        }

        cout << "Create Named pipe:" << lpszPipename << "Successfully!" << endl;

        cout << "Wating for Client Connecting..." << endl;
        ConnectNamedPipe(hPipe, NULL);
        
        cout << "Client connected, Reading msg..." << endl;
        // 
        while (1)
        {
            char szBuffer[1024] = { 0 };
            DWORD dwReadSize = 0;
            if (!ReadFile(hPipe, szBuffer, 1024, &dwReadSize, NULL))
            {
                DWORD dwLastError = GetLastError();
                if(dwLastError == ERROR_BROKEN_PIPE)
                    cout << "Client Disconnected!" << endl;
                else
                    cout << "ReadFile Error:0x" << hex << dwLastError << endl;
                break;
            }

            cout << "Recv:" << szBuffer << endl;
        }


    } while (0);

    if (hPipe)
    {
        CloseHandle(hPipe);

    }
    system("pause");
}


客户端代码:

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    HANDLE hPipe = INVALID_HANDLE_VALUE;
    const char* lpszPipename = ("\\\\.\\pipe\\namedpipe_td");

    // Connecting Pipe Server
    do
    {
        hPipe = CreateFileA(lpszPipename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
        if (hPipe != INVALID_HANDLE_VALUE)
        {
            break;
        }

        DWORD dwLastError = GetLastError();
        if (dwLastError != ERROR_PIPE_BUSY)
        {
            cout << "Could not open pipe, error: " << hex << dwLastError << endl;
            return -1;
        }

        if (!WaitNamedPipeA(lpszPipename, 20000))
        {
            cout << "Could not open pipe: 20 seconds wait timeout." << endl;
            return -1;
        }

    } while (1);

    // Connected , change to message-read mode.

    DWORD dwMode = PIPE_READMODE_MESSAGE;
    if (!SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL))
    {
        cout << "SetNamedPipeHandleState failed error:" << GetLastError() << endl;
        return -1;
    }

    cout << "Sending message to Pipe Server." << endl;

    DWORD cbWritten = 0;
    char lpvMessage[1024] = "test";
    if (!WriteFile(hPipe, lpvMessage, 1024, &cbWritten, NULL))
    {
        cout << "WriteFile to pipe failed. error:" << GetLastError() << endl;
        return -1;
    }

    system("pause");

    if (hPipe)
    {
        CloseHandle(hPipe);

    }
   
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值