gpt4 book ai didi

网络上的 C++ 命名管道无效名称错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:56 25 4
gpt4 key购买 nike

我正在尝试通过命名管道在两台 Windows 电脑之间发送消息。在本地调用 CreateNamedPipe 时一切正常。如果我将主机名从 "\\\\.\\pipe\\testpipename" 更改为 "\\\\myHostname\\pipe\\testpipename" 我得到一个ERROR_INVALID_NAME(123) 来自 getLastError()

这是我的代码:

    BOOL   fConnected = FALSE;
DWORD dwThreadId = 0;
HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL;
LPTSTR pipeName = /*nullptr*/ TEXT("\\\\myHostname\\pipe\\testpipename");

SECURITY_ATTRIBUTES sa = { 0 };
SECURITY_DESCRIPTOR sd = { 0 };

InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);

SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);

sa.bInheritHandle = false;
sa.lpSecurityDescriptor = &sd;
sa.nLength = sizeof(sa);

hPipe = CreateNamedPipe(
pipeName, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
255, // output buffer size
255, // input buffer size
0, // client time-out
&sa); // default security attribute

if (hPipe == INVALID_HANDLE_VALUE)
{
cout << GetLastError();
return -2;
}

cout << "Waiting for client to connect!" << endl;

//waiting for client to connect
fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

cout << "Client connected! YEAH" << endl;

我的猜测是管道名称无效,但我不知道为什么。有什么想法吗?

最佳答案

问题解决了!服务器和客户端的管道名称是:

Server: "\\\\.\\pipe\\testpipe"
Client: "\\\\serverHostName\\pipe\\testpipe"

客户端也做了一些小改动。完整代码可以在 my Github repo 找到.

关于网络上的 C++ 命名管道无效名称错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41380071/

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