如何使用重叠 I/O 检测客户端与命名管道服务器的连接?

发布于 2024-09-06 11:15:36 字数 547 浏览 11 评论 0原文

我正在研究使用命名管道的 MSDN 示例:

服务器可以轻松检测客户端何时断开连接并创建命名管道的实例。但我无法弄清楚服务器如何在发送来自客户端的任何数据之前知道客户端已连接到管道。

服务器可以在客户端发送任何数据之前检测到已连接的客户端吗?

如果服务器在客户端首先断开连接之前调用DisconnectNamedPipe,这也会断开客户端的连接吗? 服务器可以在不与客户端协商的情况下断开客户端与管道的连接吗?

I was studying the MSDN examples of using named pipes:

The server easily detects when the client is disconnected and creates a instance of a named pipe. But I cannot figure out how the server knows that a client is connected to a pipe before any data from client is sent.

Can server detect a connceted client before client sends any data?

If server calls DisconnectNamedPipe before client disconnects itself first, will this disconnect the client as well? Can server disconnect a client from a pipe without negotiating it with the client?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も星光 2024-09-13 11:15:36

不确定我是否理解这个问题。服务器调用 ConnectNamedPipe 等待客户端连接。无需发送任何数据。它也不能被发送,在客户端连接之前你不能发出 ReadFile。请注意,SDK 示例也使用了它。

如果服务器不正常地断开连接(没有用某种消息通知客户端,以便它可以关闭管道的末端),那么客户端将收到错误,ERROR_PIPE_NOTCONNECTED(我认为)。没有什么理由依赖于正常关闭,当管道服务器进程意外崩溃和烧毁时,您需要做一些合理的事情。

请注意,由于管道的异步特性,很难正确处理它们。出现实际上不是问题的错误是很常见的,您需要处理它。我的管道代码处理这些错误:

  • ConnectNamedPipe:连接竞争时出现 ERROR_PIPE_CONNECTED,忽略
  • FlushFileBuffers:管道关闭时出现竞争,忽略所有错误
  • WaitNamedPipe:ERROR_FILE_NOT_FOUND 如果超时已过期,则转换为 WAIT_TIMEOUT
  • CreateFile:如果另一个客户端设法先获取管道,则转换为 WAIT_TIMEOUT CreateFile:ERROR_PIPE_BUSY,重复

Not sure I understand the hang-up. The server calls ConnectNamedPipe to wait for a client connection. No data needs to be sent. Nor can it be sent, you cannot issue a ReadFile until a client is connected. Note that the SDK sample uses this as well.

If the server disconnects ungracefully (without notifying the client with some kind of message so it can close its end of the pipe) then the client will get an error, ERROR_PIPE_NOTCONNECTED (I think). There's little reason to rely on that for a normal shutdown, you need to do something reasonable when the pipe server process crashed and burned unexpectedly.

Beware that pipes are tricky to get right due to their asynchronous nature. Getting errors that are not actually problems is common and you'll need to deal with it. My pipe code deals with these errors:

  • ConnectNamedPipe: ERROR_PIPE_CONNECTED on connection race, ignore
  • FlushFileBuffers: race on pipe closure, ignore all errors
  • WaitNamedPipe: ERROR_FILE_NOT_FOUND if the timeout expired, translate to WAIT_TIMEOUT
  • CreateFile: ERROR_PIPE_BUSY if another client managed to grab the pipe first, repeat
怕倦 2024-09-13 11:15:36

当客户端在 WaitNamedPipe() 或/和 CreateFile() 调用期间收到 ERROR_FILE_NOT_FOUND 错误时,服务器无法正常工作。此错误代码意味着服务器上没有可用的指定名称的管道。您应该在 ConnectNamedPipe() 调用后立即在服务器上创建新管道以避免此问题。

Server works incorrectly when clients get ERROR_FILE_NOT_FOUND error during WaitNamedPipe() or/and CreateFile() calls. This error code means there no pipes with specified name available on server. You should create new pipe on server immediately after ConnectNamedPipe() call to avoid this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文