c - 我的管道客户端如何等待服务器
问题描述
我在 Windows 上运行的 C 中实现了一个命名管道 IPC 方案,该方案大部分都在工作。在客户端,我像这样创建管道
void CreatePipex(const LPCWSTR pipename, InteruptHandler interruptHandler, unsigned char *USARTData)
{
while (1)
{
pipes [_pipeIndex].handle = CreateFile(
pipename,
GENERIC_READ |
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
// break if pipe was created
if (pipes[_pipeIndex].handle != INVALID_HANDLE_VALUE)
{
wprintf(L"created pipe %s\n", pipename);
break;
}
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
}
// exit if pipe not created and not busy
if (GetLastError() != ERROR_PIPE_BUSY)
{
wchar_t buf[256];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
wprintf(L"Could not create %s : %s %d\n",pipename, buf,GetLastError());
//printf("could not create pipe %d\n", lastError);
exit(-1);
}
if (!WaitNamedPipe(pipename, 20000))
{
wprintf(L"Could not open %s: 20 second wait timed out.\n",pipename);
exit(-1);
}
}
DWORD mode = PIPE_NOWAIT;
if (!SetNamedPipeHandleState(pipes[_pipeIndex].handle, &mode, 0, 0))
{
wchar_t buf[256];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
wprintf(L"Could not set mode for %s : %s\n", pipename, buf);
exit(-1);
}
但是,如果我在没有服务器的情况下运行客户端,则无法创建客户端管道
Could not create \\.\pipe\F0 : The system cannot find the file specified.
有没有办法让我的客户等待服务器?
解决方案
您可以循环(和睡眠)直到 CreateFile 返回与 ERROR_FILE_NOT_FOUND 不同的内容。
推荐阅读
- influxdb - 涌入数据库 API。给定字段不为空的记录
- python - 我的自定义小部件在其他会话上对我的paintEvent 没有反应
- python - 如何比较两列以在python中获取重复项
- c# - DataGridView 中的数据在哪里更新?
- mysql - 如何在 mysql 5.7 中完全禁用 GTID?
- json - 如何按字段名在json文件中迭代
- javascript - 按空格键不会切换视频播放或暂停
- r - 我可以根据 R 中的位置拆分字符向量吗?
- azure-devops - 是否可以通过 api 获取提交更改(仅限差异)?
- android - Android处理触摸事件并将它们传递给孩子或另一个视图来处理