Windows在实践中使用命名管道 [英] Windows named pipes in practice

查看:172
本文介绍了Windows在实践中使用命名管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Windows命名管道,使用CreateNamedPipeConnectNamedPipeDisconnectNamedPipeCloseHandle调用的正确方法是什么?

With Windows named pipes, what is the proper way to use the CreateNamedPipe, ConnectNamedPipe, DisconnectNamedPipe, and CloseHandle calls?

我正在制作一个连接到客户端应用程序的服务器应用程序,该客户端应用程序在一个会话中多次连接和断开与管道的连接.

I am making a server app which is connecting to a client app which connects and disconnects to the pipe multiple times across a session.

当由于客户端断开连接而导致写操作失败时,我应该调用DisconnectNamedPipeCloseHandle还是不打任何电话.

When my writes fail because the client disconnected, should I call DisconnectNamedPipe, CloseHandle, or nothing on my handle.

然后,要接受新的连接,我应该先呼叫CreateNamedPipe然后呼叫ConnectNamedPipe,还是先呼叫ConnectNamedPipe?

Then, to accept a new connection, should I call CreateNamedPipe and then ConnectNamedPipe, or just ConnectNamedPipe?

我非常想解释这些调用导致管道处于不同状态的原因,因为我在其他地方都找不到.

I would very much like an explanation of the different states my pipe can be in as a result of these calls, because I have not found this elsewhere.

其他信息:

语言:使用win32pipewin32filewin32api库的Python.

Language: Python using the win32pipe,win32file and win32api libraries.

管道设置:等待,无重叠,字节流.

Pipe settings: WAIT, no overlap, bytestream.

推荐答案

我设法实现了自己想要的目标.我在每个会话中只调用一次CreateNamedPipeCloseHandle,当写入失败时调用DisconnectNamedPipe,然后再调用另一个ConnectNamedPipe.

I have managed to achieve what I wanted. I call CreateNamedPipe and CloseHandle exactly once per session, and I call DisconnectNamedPipe when my write fails, followed by another ConnectNamedPipe.

诀窍是仅在实际连接管道时才调用DisconnectNamedPipe.每当我尝试连接只是为了确定"时,我都会调用它,这给了我奇怪的错误.

The trick is to only call DisconnectNamedPipe when the pipe was actually connected. I called it every time I tried to connect "just to be sure" and it gave me strange errors.

有关管道的更多信息,另请参见djgandy的答案.

See also djgandy's answer for more information about pipes.

这篇关于Windows在实践中使用命名管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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