关于 Windows 中的管道行为
hPipe = CreateNamedPipe(
lpszPipename, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFSIZE, // output buffer size
BUFSIZE, // input buffer size
0,
我对此有两个问题:
- 如果上面的代码运行两次,将创建多少个管道,
1
或2
? - 如果
2
,假设其中一个管道由A连接,则B尝试连接lpszPipename
,是吗保证B会连接到没有人连接过的那个?
hPipe = CreateNamedPipe(
lpszPipename, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFSIZE, // output buffer size
BUFSIZE, // input buffer size
0,
I have two questions about this:
- what if the above code is run twice,how many pipes will get created,
1
or2
? - if
2
,suppose one of the pipe get connected by A,then B tries to connectlpszPipename
, is it guaranteed that B will connect to the one that no one has connected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果使用 FILE_FLAG_FIRST_PIPE_INSTANCE 标志,则第二次调用同名的 CreateNamedPipe 将失败;如果未使用此标志,则连接到同一管道。如果第二个 CreateNamedPipe 调用成功,它将返回同一内核对象的另一个句柄。
The second call to CreateNamedPipe with the same name fails, if FILE_FLAG_FIRST_PIPE_INSTANCE flag is used, or connects to the same pipe, if this flag is not used. In the case the second CreateNamedPipe call succeeds, it returns another handle to the same kernel object.
在 CreateNamedPipe 函数的第四个参数中,您可以限制可以创建的命名管道实例的数量。如果将其设置为 PIPE_UNLIMITED_INSTANCE (如示例所示)并调用 CreateNamedPipe 函数使用相同的参数两次,将创建两个实例(它们将共享相同的管道名称),并且两个客户端将能够连接到您的命名管道服务器(每个客户端都连接到一个命名管道实例) )。
有关详细信息,请参阅 http://msdn .microsoft.com/en-us/library/aa365594%28v=VS.85%29.aspx
In the fourth parameter of CreateNamedPipe function you can limit how many named pipe instances can be created. If you set it to PIPE_UNLIMITED_INSTANCE as in your example and call the CreateNamedPipe function twice with the same parameters, two instances will be created (they'll share the same pipe name) and two clients will be able to connect to your named pipe server (each of them to one named pipe instance).
For more information look at http://msdn.microsoft.com/en-us/library/aa365594%28v=VS.85%29.aspx