简体   繁体   English

C ++:使用Win32 API实现命名管道

[英]C++: Implementing Named Pipes using the Win32 API

I'm trying to implement named pipes in C++, but either my reader isn't reading anything, or my writer isn't writing anything (or both). 我正在尝试用C ++实现命名管道,但要么我的读者不读任何东西,要么我的作者不写任何东西(或两者兼而有之)。 Here's my reader: 这是我的读者:

int main()
{
    HANDLE pipe = CreateFile(GetPipeName(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);

    char data[1024];
    DWORD numRead = 1;

    while (numRead >= 0)
    {
        ReadFile(pipe, data, 1024, &numRead, NULL);

        if (numRead > 0)
            cout << data;
    }

    return 0;
}

LPCWSTR GetPipeName()
{
    return L"\\\\.\\pipe\\LogPipe";
}

And here's my writer: 这是我的作家:

int main()
{
    HANDLE pipe = CreateFile(GetPipeName(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);

    string message = "Hi";
    WriteFile(pipe, message.c_str(), message.length() + 1, NULL, NULL); 

    return 0;
}

LPCWSTR GetPipeName()
{
    return L"\\\\.\\pipe\\LogPipe";
}

Does that look right? 那看起来不错吗? numRead in the reader is always 0, for some reason, and it reads nothing but 1024 -54's (some weird I character). 由于某种原因,阅读器中的numRead始终为0,并且它只读取1024 -54(我有些奇怪的字符)。

Solution: 解:

Reader (Server): 读者(服务器):

while (true)
{
    HANDLE pipe = CreateNamedPipe(GetPipeName(), PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND , PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL);

    if (pipe == INVALID_HANDLE_VALUE)
    {
        cout << "Error: " << GetLastError();
    }

    char data[1024];
    DWORD numRead;

    ConnectNamedPipe(pipe, NULL);

    ReadFile(pipe, data, 1024, &numRead, NULL);

    if (numRead > 0)
        cout << data << endl;

    CloseHandle(pipe);
}

Writer (client): 作家(客户):

HANDLE pipe = CreateFile(GetPipeName(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if (pipe == INVALID_HANDLE_VALUE)
{
    cout << "Error: " << GetLastError();
}

string message = "Hi";

cout << message.length();

DWORD numWritten;
WriteFile(pipe, message.c_str(), message.length(), &numWritten, NULL); 

return 0;

The server blocks until it gets a connected client, reads what the client writes, and then sets itself up for a new connection, ad infinitum. 服务器阻塞,直到它获得连接的客户端,读取客户端写入的内容,然后无限制地为自己设置新连接。 Thanks for the help, all! 谢谢大家的帮助!

You must use CreateNamedPipe() to create the server end of a named pipe. 您必须使用CreateNamedPipe()来创建命名管道的服务器端。 Be sure to specify a non-zero buffer size, zero (documented by MSDN as 'use system default buffer size') doesn't work. 请务必指定非零缓冲区大小,零(由MSDN记录为“使用系统默认缓冲区大小”)不起作用。 MSDN has decent samples for a multi-threaded client&server. MSDN为多线程客户端和服务器提供了不错的示例

A named pipe client can open the named pipe with CreateFile -- but the named pipe server needs to use CreateNamedPipe to create the named pipe. 命名管道客户端可以使用CreateFile打开命名管道 - 但命名管道服务器需要使用CreateNamedPipe来创建命名管道。 After it's created the named pipe, the server uses ConnectNamedPipe to wait for a client to connect. 在创建命名管道后,服务器使用ConnectNamedPipe等待客户端连接。 Only after the client has connected should the server do a blocking read like your call to ReadFile . 只有客户端连接 ,服务器才会执行阻塞读取,就像调用ReadFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM