我被困在这里,请帮助。我有一个 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 文件标志。管道缓冲区已满?我一直从服务器端的管道读取。
非常感谢!