命名管道(Named Pipes)通信学习

学习内容出处:《Windows网络编程技术》第4章命名管道


命名管道的基本原理:利用微软网络提供者(MSNP)重定向器。
特点:
  • 跨网络。
  • 可靠的传输。
  • 单向或双向数据通信。


服务端源码,PipeServer.cpp。
// PipeServer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>

//最多创建实例数
#define NUM_PIPES 1
//管道名称
#define PIPE_NAME _T("\\\\.\\Pipe\\Jim")

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE PipeHandle = NULL;
    DWORD BytesRead = 0;
    TCHAR buffer[256] = {0};

    PipeHandle = CreateNamedPipe(PIPE_NAME, PIPE_ACCESS_DUPLEX,
        PIPE_TYPE_BYTE|PIPE_READMODE_BYTE, NUM_PIPES, 0, 0, 1000, NULL);
    if (PipeHandle == INVALID_HANDLE_VALUE)
    {
        printf("CreateNamedPipe failed with error %d\n", GetLastError());
        return 0;
    }

    printf("Server is now running\n");

    //一直等待,直到客户端连接
    if (ConnectNamedPipe(PipeHandle, NULL) == 0)
    {
        printf("ConnectNamedPipe failed with error %d\n", GetLastError());
        CloseHandle(PipeHandle);
        return 0;
    }

    //发送消息到客户端
    DWORD BytesWritten;
    if (WriteFile(PipeHandle, "Server Call you", 15, &BytesWritten, NULL) == 0)
    {
        printf("WriteFile failed with error %d\n", GetLastError());
        CloseHandle(PipeHandle);
        return 0;
    }

    //等待客户端消息
    if (ReadFile(PipeHandle, buffer, sizeof(buffer), &BytesRead, NULL) <= 0)
    {
        printf("ReadFile failed with error %d\n", GetLastError());
        CloseHandle(PipeHandle);
        return 0;
    }

    printf("%.*s\n", BytesRead, buffer);

    if (DisconnectNamedPipe(PipeHandle) == 0)
    {
        printf("DisconnectNamedPipe failed with error %d\n", GetLastError());
        return 0;
    }

    CloseHandle(PipeHandle);

	return 0;
}


客户端源码,PipeClient.cpp。

// PipeClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>

//管道名称
#define PIPE_NAME _T("\\\\.\\Pipe\\Jim")

int _tmain(int argc, _TCHAR* argv[])
{
    if (WaitNamedPipe(PIPE_NAME, NMPWAIT_WAIT_FOREVER) == 0)
    {
        printf("WaitNamedPipe failed with error %d\n", GetLastError());
        return 0;
    }

    //Create the named pipe file handle
    HANDLE PipeHandle = CreateFile(PIPE_NAME, GENERIC_READ|GENERIC_WRITE, 0,
        (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
    if (PipeHandle == INVALID_HANDLE_VALUE)
    {
        printf("CreateFile failed with error %d\n", GetLastError());
        return 0;
    }

    //接受服务端消息
    DWORD BytesRead = 0;
    TCHAR buffer[256] = {0};
    if (ReadFile(PipeHandle, buffer, sizeof(buffer), &BytesRead, NULL) <= 0)
    {
        printf("ReadFile failed with error %d\n", GetLastError());
        CloseHandle(PipeHandle);
        return 0;
    }
    printf("%.*s\n", BytesRead, buffer);

    //发送消息到服务端
    DWORD BytesWritten;
    if (WriteFile(PipeHandle, "Get it, sir!", 12, &BytesWritten, NULL) == 0)
    {
        printf("WriteFile failed with error %d\n", GetLastError());
        CloseHandle(PipeHandle);
        return 0;
    }

    printf("Wrote %d bytes\n", BytesWritten);
    CloseHandle(PipeHandle);

	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Windows 中,可以使用命名管道Named Pipes)实现进程间通信。以下是一些基本步骤: 1. 创建命名管道 使用 CreateNamedPipe 函数创建一个命名管道。该函数需要指定管道名称、管道的读写模式、管道的最大实例数等参数。 2. 等待客户端连接 使用 ConnectNamedPipe 函数等待客户端的连接。该函数会一直阻塞,直到有客户端连接成功。 3. 接收客户端数据 使用 ReadFile 函数从管道中读取客户端发送的数据。 4. 发送数据给客户端 使用 WriteFile 函数向管道中写入数据,以便客户端读取。 5. 断开连接 使用 DisconnectNamedPipe 函数断开与客户端的连接。如果需要与多个客户端通信,则返回第 2 步。 6. 关闭管道 使用 CloseHandle 函数关闭命名管道的句柄。 注意事项: - 在创建管道时,需要指定管道名称,该名称在系统中必须是唯一的。 - 管道支持同步和异步方式进行读写操作,可以根据具体需求选择使用哪种方式。 - 管道的读写操作是阻塞式的,也可以使用 overlapped 结构体实现异步操作。 下面是一个简单的代码示例,演示如何使用命名管道实现进程间通信: ``` #include <windows.h> #include <stdio.h> #define PIPE_NAME "\\\\.\\pipe\\MyPipe" int main() { HANDLE hPipe; char buffer[1024]; DWORD dwRead; // 创建命名管道 hPipe = CreateNamedPipe(PIPE_NAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 0, 0, 0, NULL); if (hPipe == INVALID_HANDLE_VALUE) { printf("CreateNamedPipe failed! Error code: %d\n", GetLastError()); return 1; } // 等待客户端连接 if (!ConnectNamedPipe(hPipe, NULL)) { printf("ConnectNamedPipe failed! Error code: %d\n", GetLastError()); return 1; } // 接收客户端数据 if (!ReadFile(hPipe, buffer, sizeof(buffer), &dwRead, NULL)) { printf("ReadFile failed! Error code: %d\n", GetLastError()); return 1; } printf("Received data from client: %s\n", buffer); // 发送数据给客户端 if (!WriteFile(hPipe, "Hello, client!", 15, NULL, NULL)) { printf("WriteFile failed! Error code: %d\n", GetLastError()); return 1; } // 断开连接 DisconnectNamedPipe(hPipe); // 关闭管道 CloseHandle(hPipe); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值