【发布时间】:2016-04-08 17:57:59
【问题描述】:
我有一个 C# 服务器和一个 C++ 客户端。 C# 服务器工作得很好,因为我使用 C# 客户端对其进行了测试,并且来自客户端的所有消息都已正确传输到服务器。但是,在实现 C++ 客户端时,我似乎遇到了某些问题。
首先,这是我的 C# 服务器:
Task.Factory.StartNew(() =>
{
NamedPipeServerStream server = new NamedPipeServerStream(name);
server.WaitForConnection();
StreamReader reader = new StreamReader(server);
Console.WriteLine("Client connected");
while (true)
{
if (reader.ReadLine() != null)
Console.WriteLine(reader.ReadLine());
}
});
name 变量作为参数传递给方法。
我的 C++ 代码如下:
HANDLE hPipe;
DWORD dwWritten;
hPipe = CreateFile(TEXT("\\\\.\\pipe\\testpipe"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hPipe != INVALID_HANDLE_VALUE)
{
WriteFile(hPipe,
"Hello Pipe\n",
12, // = length of string + terminating '\0' !!!
&dwWritten,
NULL);
CloseHandle(hPipe);
}
服务器的输出如下:
[PIPE] Client connected
[PIPE]
有人有什么建议吗?也许我完全错了,因为我对管道完全陌生。
提前致谢。
【问题讨论】:
-
您是否尝试过在关闭 C++ 端的句柄之前刷新文件缓冲区? msdn.microsoft.com/en-us/library/windows/desktop/…
-
我在调用 WriteFile() 后添加了 FlushFileBuffers(hPipe) 但仍然没有。
标签: c# c++ named-pipes