使用NamedPipe时,是否可以检测到客户端管道的句柄是否关闭? [英] Is there any way to detect whether client pipe's handle is closed when using NamedPipe?

查看:254
本文介绍了使用NamedPipe时,是否可以检测到客户端管道的句柄是否关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在Windows平台上从服务器端检测客户端管道句柄的状态.

I am wondering if there is a way to detect the status of client pipe's handle from the server side on Windows platform.

即使客户端使用CloseHandle()功能关闭了管道(已断开连接),似乎也无法从服务器端对其进行检测.

Even though the client closed the pipe(disconnected) with CloseHandle() function, there seems to be no way to detect it from the server side.

WaitForSingleObject()与句柄对象一起使用将返回WAIT_OBJECT_0,而不管客户端句柄的状态如何.那么,从服务器端以较低的成本检测客户端管道的句柄是否关闭的一个好的解决方案是什么呢?

Using WaitForSingleObject() with handle object returns WAIT_OBJECT_0, regardless of the status of client handle. So, what would be a good solution to detect whether client pipe's handle is closed or not from server side with less cost?

推荐答案

如果您正在从管道(PIPE_ACCESS_INBOUNDPIPE_ACCESS_DUPLEX)读取数据,则当客户端关闭管道末端时,您将得到ERROR_BROKEN_PIPE.即使您还没有准备好处理管道中的数据,也可以提前(使用异步I/O)开始读取数据,以检测管道何时断开.

If you are reading data from the pipe (PIPE_ACCESS_INBOUND or PIPE_ACCESS_DUPLEX) you will get ERROR_BROKEN_PIPE when the client closes their end of the pipe. Even if you aren't ready to process data from the pipe you could start reading it ahead of time (using asynchronous I/O) in order to detect when the pipe is broken.

请注意,如果管道客户端有多个句柄,则只有在关闭最后一个句柄时,它才会被视为已关闭.例如,如果客户端在不经意间导致子进程继承了句柄的副本,则可能是一个问题.

Note that if there is more than one handle to the client end of the pipe, it will only be considered closed when the last handle to it is closed. This might be an issue, for example, if the client inadvertently causes a subprocess to inherit a copy of the handle.

我不知道有什么方法可以检测到客户端已关闭仅出站管道而不向其写入数据.最好的选择可能是使用PIPE_ACCESS_DUPLEX,即使管道的进入端仅用于检测管道何时断裂.

I don't know of any way to detect that the client has closed an outbound-only pipe without writing data to it. The best option may be to use PIPE_ACCESS_DUPLEX even if the incoming side of the pipe is only ever used to detect when the pipe has been broken.

供以后参考,尽管可以将句柄用作同步对象,但不建议这样做,据我所知,唯一受支持的用途是检测没有事件对象的异步I/O操作的完成.指定.

For future reference, although a handle can be used as a synchronization object, doing so is not recommended and as far as I know the only supported use is to detect the completion of an asynchronous I/O operation where no event object was specified.

这篇关于使用NamedPipe时,是否可以检测到客户端管道的句柄是否关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆