C++/C#:在关闭管道句柄之前,管道WriteFile()不会将数据写入接收器端 < >我试图用C++代码模块使用 >命名管道< /C> >将代码>字符串Stry>代码>数据写入.C模块。p>
我的代码如下: C#(管道服务器): C++(管道客户端):C++/C#:在关闭管道句柄之前,管道WriteFile()不会将数据写入接收器端 < >我试图用C++代码模块使用 >命名管道< /C> >将代码>字符串Stry>代码>数据写入.C模块。p>,c#,c++,pipe,named-pipes,language-interoperability,C#,C++,Pipe,Named Pipes,Language Interoperability,我的代码如下: C#(管道服务器): C++(管道客户端): #包括“stdafx.h” #包括 #包括 int _tmain(int argc,_TCHAR*argv[] { 处理文件; 布尔flg; 德沃德·德维特; 字符更新[200]; hFile=CreateFile(L“\\.\\pipe\\samplePipe”,通用写入, 0,空,打开\u现有, 0,空); strcpy(szPipeUpdate,“createnamedpipe的命名管道客户端的数据”); if(hFile==无
#包括“stdafx.h”
#包括
#包括
int _tmain(int argc,_TCHAR*argv[]
{
处理文件;
布尔flg;
德沃德·德维特;
字符更新[200];
hFile=CreateFile(L“\\.\\pipe\\samplePipe”,通用写入,
0,空,打开\u现有,
0,空);
strcpy(szPipeUpdate,“createnamedpipe的命名管道客户端的数据”);
if(hFile==无效的句柄值)
{
DWORD dw=GetLastError();
printf(“命名管道客户端的CreateFile失败\n:”);
}
其他的
{
而(1)
{
flg=WriteFile(hFile,szPipeUpdate,strlen(szPipeUpdate),&dwWrite,NULL);
如果(FALSE==flg)
{
printf(“命名管道客户端的WriteFile失败\n”);
}
其他的
{
printf(“命名管道客户端的WriteFile成功\n”);
}
FlushFileBuffers(hFile);
//闭合手柄(hFile);
}
}
getchar();
返回0;
}
WriteFile()
将立即返回,但C#服务器未获取任何写入的管道数据。如果我在C++代码中取消了代码> CuffeHooLeIle()/Cuff>,则C语言服务器得到管道数据。我不希望每次向管道中写入数据时都关闭和打开管道。是否有任何方法可以让我只打开管道一次,然后在需要将数据写入管道时执行WriteFile()
在写入管道时,是否使用换行符结束数据?因为在C#服务器代码中,您使用ReadLine,它将阻塞,直到它接收到换行符(或直到您关闭管道)。他不-strcpy(szPipeUpdate,“createnamedpipe的命名管道客户端的数据”)代码>。此外,如果在每次写入之后确实需要<代码> FlushFileBuffers <代码>,请考虑使用未缓冲的I/O <代码> FielyFrasgNosiBuffel.FielyFraskRead Eng/<代码>谢谢!我找到了问题的原因。它是由StreamReader引起的,它试图缓冲过多的数据。这不是正确的方法,如果您想以“数据包”的形式传输数据,那么应该将管道更改为消息模式。如果我想以结构的形式传输数据,该怎么办?
namespace CSPipe
{
class Program
{
public void ThreadStartServer()
{
// Create a name pipe
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("samplePipe"))
{
Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());
// Wait for a connection
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");
using (StreamReader sr = new StreamReader(pipeStream))
{
string temp;
// We read a line from the pipe and print it together with the current time
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("{0}: {1}", DateTime.Now, temp);
}
}
}
Console.WriteLine("Connection lost");
Console.ReadKey();
}
static void Main(string[] args)
{
Program Server = new Program();
Thread ServerThread = new Thread(Server.ThreadStartServer);
ServerThread.Start();
}
}
}
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hFile;
BOOL flg;
DWORD dwWrite;
char szPipeUpdate[200];
hFile = CreateFile(L"\\\\.\\pipe\\samplePipe", GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
0, NULL);
strcpy(szPipeUpdate,"Data from Named Pipe client for createnamedpipe");
if(hFile == INVALID_HANDLE_VALUE)
{
DWORD dw = GetLastError();
printf("CreateFile failed for Named Pipe client\n:" );
}
else
{
while(1)
{
flg = WriteFile(hFile, szPipeUpdate, strlen(szPipeUpdate), &dwWrite, NULL);
if (FALSE == flg)
{
printf("WriteFile failed for Named Pipe client\n");
}
else
{
printf("WriteFile succeeded for Named Pipe client\n");
}
FlushFileBuffers(hFile);
// CloseHandle(hFile);
}
}
getchar();
return 0;
}