WriteFile() 块(通过命名管道从 C++ 客户端写入到 C# 服务器)

发布于 2024-11-08 07:40:00 字数 1426 浏览 0 评论 0原文

我被困在这里,请帮忙。 我有一个 C# 命名管道服务器,管道是通过以下方式创建的:

new NamedPipeServerStream(pipeName, PipeDirection.InOut, numThreads);

在 C++ 中,我像这样创建了客户端:

        m_hPipe = CreateFile( 
        strPipeName,            // Pipe name 
        GENERIC_READ |  GENERIC_WRITE,  // Read and write access 
        0,              // No sharing 
        NULL,               // Default security attributes
        OPEN_EXISTING,          // Opens existing pipe 
        FILE_FLAG_OVERLAPPED,    
        NULL);  

我将管道类型设置为 PIPE_READMODE_BYTE | PIPE_TYPE_BYTE
我编写了一个函数 WriteString() 来将字符串写入管道。该函数大致是这样的:

    // Write the length of the string to the pipe (using 2 bytes)
    bool bResult = WriteFile(m_hPipe, buf, 2, &cbBytesWritten, NULL);

    // Write the string itself to the pipe
    bResult = WriteFile(m_hPipe, m_chSend, len, &cbBytesWritten, NULL);

    // Flush the buffer
    FlushFileBuffers(m_hPipe);

我对该函数进行了两次调用:

    WriteString(_T("hello server!"));   // message sent and the server saw it correctly
    WriteString(_T("goodbye server!")); // message not sent, coz WriteFile() blocked here

现在的问题是:程序在第二次 WriteString() 调用中的第一个 WriteFile() 调用处被阻塞。 只有当服务器关闭管道时,WriteFile() 调用才会返回错误。

这是可以可靠地重现的。

是什么阻止了 WriteFile() 调用?我已经在使用 OVERLAPPED 文件标志。 管道缓冲区已满?我不断从服务器端的管道中读取数据。

多谢!

I am stuck here, please help.
I have a C# named pipe server, the pipe is created by:

new NamedPipeServerStream(pipeName, PipeDirection.InOut, numThreads);

In C++ I created the client like this:

        m_hPipe = CreateFile( 
        strPipeName,            // Pipe name 
        GENERIC_READ |  GENERIC_WRITE,  // Read and write access 
        0,              // No sharing 
        NULL,               // Default security attributes
        OPEN_EXISTING,          // Opens existing pipe 
        FILE_FLAG_OVERLAPPED,    
        NULL);  

I set the pipe type to PIPE_READMODE_BYTE | PIPE_TYPE_BYTE
I wrote a function WriteString() to write a string to the pipe. The function is roughly like this:

    // Write the length of the string to the pipe (using 2 bytes)
    bool bResult = WriteFile(m_hPipe, buf, 2, &cbBytesWritten, NULL);

    // Write the string itself to the pipe
    bResult = WriteFile(m_hPipe, m_chSend, len, &cbBytesWritten, NULL);

    // Flush the buffer
    FlushFileBuffers(m_hPipe);

I made two calls to the function:

    WriteString(_T("hello server!"));   // message sent and the server saw it correctly
    WriteString(_T("goodbye server!")); // message not sent, coz WriteFile() blocked here

Now the problem is: the program is blocked at the first WriteFile() call in the second WriteString() call.
Only when the pipe is closed by the server can the WriteFile() call return with an error.

This is reliably reproducible.

What is blocking the WriteFile() call here? I am already using the OVERLAPPED file flag.
Pipe buffer is full? I keep reading from the pipe on the server side.

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2024-11-15 07:40:00

FILE_FLAG_OVERLAPPED 启用异步 I/O。它不会自动使任何操作异步、非阻塞或缓冲。

您需要使用 WriteFile 的第 5 个参数 - 传入一个 OVERLAPPED 结构,或者在完成时设置一个事件,或者将文件句柄与 I/O 完成端口

FILE_FLAG_OVERLAPPED enables asynchronous I/O. It does not automatically make any operations asynchronous, non-blocking, or buffered.

You need to use the 5th parameter of WriteFile – pass in an OVERLAPPED structure, either with an event to set on completion, or associate the file handle with an I/O Completion Port.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文