如何使用重叠 I/O 检测客户端与命名管道服务器的连接?
我正在研究使用命名管道的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定我是否理解这个问题。服务器调用 ConnectNamedPipe 等待客户端连接。无需发送任何数据。它也不能被发送,在客户端连接之前你不能发出 ReadFile。请注意,SDK 示例也使用了它。
如果服务器不正常地断开连接(没有用某种消息通知客户端,以便它可以关闭管道的末端),那么客户端将收到错误,ERROR_PIPE_NOTCONNECTED(我认为)。没有什么理由依赖于正常关闭,当管道服务器进程意外崩溃和烧毁时,您需要做一些合理的事情。
请注意,由于管道的异步特性,很难正确处理它们。出现实际上不是问题的错误是很常见的,您需要处理它。我的管道代码处理这些错误:
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:
当客户端在
WaitNamedPipe()
或/和CreateFile()
调用期间收到ERROR_FILE_NOT_FOUND
错误时,服务器无法正常工作。此错误代码意味着服务器上没有可用的指定名称的管道。您应该在ConnectNamedPipe()
调用后立即在服务器上创建新管道以避免此问题。Server works incorrectly when clients get
ERROR_FILE_NOT_FOUND
error duringWaitNamedPipe()
or/andCreateFile()
calls. This error code means there no pipes with specified name available on server. You should create new pipe on server immediately afterConnectNamedPipe()
call to avoid this issue.