gpt4 book ai didi

c++ - 命名管道无法第二次连接

转载 作者:行者123 更新时间:2023-11-28 05:17:46 24 4
gpt4 key购买 nike

<分区>

我有一个客户端-服务器管道进程,当我第一次尝试连接时,一切正常,但是当我再次启动我的客户端进程而不重新启动服务器时,我无法连接并出现 PIPE_BUSY 错误,尽管 GetOverlappedResult 在服务器,但 waitforsingleobject 不会发出有人连接的信号,消息当然是空的。

std::wstring buffer;
while (true) {
// pinging here...
DWORD result = WaitForSingleObject(hEvent, 2000);

if (result == WAIT_FAILED) {
std::wcout << result << std::endl;
break;
}

if (result == WAIT_TIMEOUT) {
// always here
}

ResetEvent(hEvent);

DWORD bytesTransferred;

OVERLAPPED ov;
ZeroMemory( & ov, sizeof(OVERLAPPED));
ov.hEvent = hEvent;

if (GetOverlappedResult(pipe(), & ov, & bytesTransferred, TRUE) == 0) // FAILED
{
return; // we do never get here...
} else {
ZeroMemory( & ov, sizeof(OVERLAPPED));

DWORD bytesToRead = 0;

BOOL pingPipe;

Timer timer(1000);

while (!bytesToRead || !pingPipe) {
if (!timer.in()) {
break;
}

pingPipe = PeekNamedPipe(pipe(), NULL, 0, NULL, & bytesToRead, NULL);
}

buffer.resize(sizeof(TCHAR) * bytesToRead);

if (!ReadFile(pipe(), & buffer[0], bytesToRead, & bytesTransferred, & ov) && timer.in()) {
if (GetLastError() == ERROR_IO_PENDING) {
continue;
}

return;
}

// data here
std::wcout << buffer << std::endl;
}
}

DisconnectNamedPipe(pipe());

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