gpt4 book ai didi

.net - NamedpipeClientStream 无法连接,一直超时

转载 作者:可可西里 更新时间:2023-11-01 09:30:31 29 4
gpt4 key购买 nike

我有一个主要基于以下内容编写的 Namedpipeserver:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx
当我尝试使用此代码从 .NET 客户端连接到它时:

NamedPipeClientStream clientPipe = new NamedPipeClientStream(".",      
"\\\\.\\pipe\\TTCUIHELPER_SEND_TTC_RECEIVE",PipeDirection.Out);
try
{

if (clientPipe != null)
{
clientPipe.Connect(5000);
if (clientPipe.IsConnected == true)
{
byte[] bytes = pm.GetMessageData();
clientPipe.Write(bytes, 0, bytes.Length);
clientPipe.Flush();
clientPipe.Dispose();
clientPipe.Close();
}
}
}
catch (Exception Ex)
{
System.Windows.MessageBox.Show(Ex.Message);
}

然后连接总是超时。奇怪的是,如果我尝试使用 CPP 编写测试客户端:

LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\TTCUIHELPER_SEND_TTC_RECEIVE");
const TCHAR* lpvMessage=L"QQQQ";

HANDLE hPipe = CreateFile(
lpszPipename, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file

// Break if the pipe handle is valid.

DWORD cbWritten;
DWORD cbToWrite = (lstrlen(lpvMessage)+1)*sizeof(TCHAR);
_tprintf( TEXT("Sending %d byte message: \"%s\"\n"), cbToWrite, lpvMessage);

BOOL fSuccess = WriteFile(
hPipe, // pipe handle
lpvMessage, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL); // not overlapped

if ( ! fSuccess)
{
_tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() );
return -1;
}

然后就可以完美运行了。它适用于 CPP 而不是 .NET,我可能做错了什么?

最佳答案

只需从您的管道名称中删除管道前缀,即仅使用名称本身:

"TTCUIHELPER_SEND_TTC_RECEIVE"

.NET NamedPipeClientStream 类在内部调用 Windows WriteFile API 时为您添加前缀。

关于.net - NamedpipeClientStream 无法连接,一直超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12759618/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com