首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的管道客户端如何等待服务器

我的管道客户端如何等待服务器
EN

Stack Overflow用户
提问于 2019-11-13 18:16:31
回答 1查看 127关注 0票数 0

我正在用运行在windows上的C语言实现一个命名管道IPC方案,它基本上是有效的。在客户端,我像这样创建管道

代码语言:javascript
复制
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);
   }

但是,如果我在没有服务器的情况下运行客户端,则无法创建客户端管道

代码语言:javascript
复制
Could not create \\.\pipe\F0 : The system cannot find the file specified.

有没有办法让我的客户端等待服务器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-13 18:36:11

您可以循环(并休眠),直到CreateFile返回与ERROR_FILE_NOT_FOUND不同的内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58834830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档