How to make a named pipe not busy after client has disconnected?

前端 未结 2 1671
旧巷少年郎
旧巷少年郎 2021-02-06 17:10

I use a named pipe and I want to reuse the same pipe on the server to allow connecting another client once the original client has disconnected. What I do is:

  • serv
2条回答
  •  不要未来只要你来
    2021-02-06 17:58

    Experimenting with various calls, I have found following to work fine:

    In reaction to ERROR_PIPE_NOT_CONNECTED, server should perform:

      // allow connecting, no wait
      DWORD mode = PIPE_NOWAIT;
      SetNamedPipeHandleState(_callstackPipe,&mode,NULL,NULL);
      ConnectNamedPipe(_callstackPipe,NULL);
      mode = PIPE_WAIT;
      SetNamedPipeHandleState(_callstackPipe,&mode,NULL,NULL);
    

    ConnectNamedPipe makes the pipe connectable (not busy) again.

    Note: pipe state is changed temporarily to PIPE_NOWAIT, as otherwise ConnectNamedPipe blocks the server thread waiting for the client infinitely.

    Other solution could probably be to close the handle completely on the server side and open it again.

提交回复
热议问题