Win32 API用于服务器-客户端管道通信的顺序是什么在C ++中CreateNamedPipe,WriteFile,CreateFile,ReadFile

问题描述

我试图使用Win32 API在Visual Studio 2019中用C ++编写服务器/客户端程序。

这是相关文档:Named Pipe Open Modes

我使用了4个API:

在服务器端(创建管道并对其进行写操作的一方):CreateNamedPipe()WriteFile()

在客户端(通过管道进行连接和读取):CreateFile()ReadFile()

但是,我发现服务器无法写入管道。

以下是我使用的代码。

Servermain.cpp

#include <iostream>
#include <windows.h>

using namespace std;

void namedPipeServer()
{
    HANDLE hPipeServer;
    char Wbuffer[1024] = "Hello,from the pipe server!";
    DWORD dwWrite;
    BOOL writeSuccessFlag;

    //Create a named pipe
    hPipeServer = CreateNamedPipe(
        TEXT("\\\\.\\pipe\\Agentpipe"),//lpName
        PIPE_ACCESS_OUTBOUND,//dwOpenMode
        PIPE_TYPE_BYTE,//dwPipeMode
        1,//nMaxInstances
        1024 * 16,//nOutBufferSize
        1024 * 16,//nInBufferSize
        NMPWAIT_USE_DEFAULT_WAIT,//nDefaultTimeOut
        NULL);                     //lpSecurityAttributes

    cout << "Inside namedPipeServer()" << endl;

    if (hPipeServer != INVALID_HANDLE_VALUE)
    {
        cout << "Just writing to pipe" << endl;

        writeSuccessFlag = WriteFile(
            hPipeServer,//HANDLE       hFile
            Wbuffer,//LPCVOID      lpBuffer
            30,//DWORD        nNumberOfBytesToWrite
            &dwWrite,NULL         //LPOVERLAPPED lpOverlapped
        );

        if (writeSuccessFlag)
        {
            cout << "Server has written to pipe!" << endl;
        }
        else
        {
            cout << "Unsuccessful write to pipe,From Agent" << endl;
        }
    }
    else
    {
        cout << "Unsuccesful pipe connection. hPipeServer: " << hPipeServer << endl;
    }
}

int main()
{
    cout << "Inside Agent server. Creating a named pipe.\n" << endl;
    namedPipeServer();
    while (1);
    return 0;
}

Clientmain.cpp:

#include <iostream>
#include <windows.h>

using namespace std;

void readFromPipe()
{
    HANDLE hPipeClient;
    char rBuffer[1024];
    DWORD dwRead;
    BOOL readSuccessFlag = 0;

    //Connect to the server pipe: \\.\\pipe\\Agentpipe
    cout << "Inside readFromPipe()." << endl;

    hPipeClient = CreateFile(
        TEXT("\\\\.\\pipe\\Agentpipe"),//lpFileName
        GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL
    );

    while (hPipeClient != INVALID_HANDLE_VALUE)
    {
        cout << "Just connecting to pipe" << endl;
        readSuccessFlag = ReadFile(
            hPipeClient,//HANDLE       hFile,rBuffer,//LPVOID       lpBuffer,30,//DWORD        nNumberOfBytesToRead,&dwRead,//LPDWORD      lpNumberOfBytesRead,NULL         //LPOVERLAPPED lpOverlapped
        );

        if (readSuccessFlag)
        {
            cout << "Client has read from pipe of Agent!" << endl;
            cout << "From Agent Pipe: " << rBuffer << endl;
        }
        else
        {
            cout << "Unsuccessful Pipe read!" << endl;
        }
    }
    if(hPipeClient == INVALID_HANDLE_VALUE)
    {
        cout << "Unsuccesful pipe connection at client end. hPipeClient: " << hPipeClient << endl;
    }
}

int main()
{
    cout << "Inside the client. Calling readFromPipe()" << endl;
    readFromPipe();
    while (1);
    return 0;
}

执行上述程序后,表明服务器无法写入管道,并且服务器端的输出为:

Inside Agent server. Creating a named pipe.
Inside namedPipeServer()
Just writing to pipe
Unsuccessful write to pipe,From Agent

客户端控制台上的输出为:

Inside the client. Calling readFromPipe()
Inside readFromPipe().
Just connecting to pipe

在查看Win32文档中的示例程序时,我发现这些Win32 API的使用顺序不同,如下所示:

Pipe Server程序:

main(){
    ...
    namedPipeServer()
    ...
}

void namedPipeServer()
{
    ...
    CreateFile()
    WriteFile()
    ...
}

Pipe Client程序:

main(){
    ...
    readFromPipe()
    ...
}

void readFromPipe()
{
    ...
    CreateNamedPipe()
    ReadFile()
    ...
}

如果有人可以特别明确地说明我对CreateNamedPipe()CreateFile()的使用,我将感到高兴。

服务器是否必须首先使用CreateFile()(在创建管道之前,先将其写入),还是可以使用CreateNamedPipe()

MY 程序中使用API​​的顺序发布不正确吗?如果是这样,请说明原因。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...