如何从 Java 打开 Windows 命名管道?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/634564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a Windows named pipe from Java?
提问by Philipp
On our Linux system we use named pipes for interprocess communication (a producer and a consumer).
在我们的 Linux 系统上,我们使用命名管道进行进程间通信(生产者和消费者)。
In order to test the consumer (Java) code, I would like to implement (in Java) a dummy producer which writes to a named pipe which is connected to the consumer.
为了测试消费者(Java)代码,我想(在Java中)实现一个虚拟生产者,该生产者写入连接到消费者的命名管道。
Now the test should also work in the Windows development environment. Thus I would like to know how to create a named pipe in Windows from Java. In Linux I can use mkfifo (called using Runtime.exec()
), but how should I do this on Windows?
现在测试应该也可以在 Windows 开发环境中工作。因此,我想知道如何从 Java 在 Windows 中创建命名管道。在 Linux 中,我可以使用 mkfifo(称为 using Runtime.exec()
),但在 Windows 上我应该如何使用?
采纳答案by v01ver
Use Named Pipes to Communicate Between Java and .Net Processes
Relevant part in the link
链接中的相关部分
try {
// Connect to the pipe
RandomAccessFile pipe = new RandomAccessFile("\\.\pipe\testpipe", "rw");
String echoText = "Hello word\n";
// write to pipe
pipe.write ( echoText.getBytes() );
// read response
String echoResponse = pipe.readLine();
System.out.println("Response: " + echoResponse );
pipe.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
回答by Michael Borgwardt
In windows, named pipes existbut they cannot be created as files in a writeable filesystemand there is no command line tool. They live in a special filesystem and can be created only by using the Win32 API.
在 Windows 中,存在命名管道,但它们不能在可写文件系统中创建为文件,并且没有命令行工具。它们存在于一个特殊的文件系统中,只能通过使用 Win32 API 来创建。
Looks like you'll have to resort to native code, or switch from pipes to sockets for IPC - probably the best longterm solution, since it's much more portable.
看起来您将不得不求助于本机代码,或者从管道切换到 IPC 的套接字 - 可能是最好的长期解决方案,因为它更便携。
回答by rogerdpack
maybe could use cygwin named pipes--if all your processes are cygwin.
也许可以使用 cygwin 命名管道——如果你所有的进程都是 cygwin。
回答by shellster
It is very much possible to read and write to an existingnamed pipe in Java. You cannot, to my knowledge, create a named pipe in a Windows environment. Linux is a different story as named pipes can be created and consumed like files.
在 Java 中读取和写入现有的命名管道是非常可能的。据我所知,您不能在 Windows 环境中创建命名管道。Linux 是一个不同的故事,因为命名管道可以像文件一样被创建和使用。
Relevant link on interacting with an existing pipe: http://v01ver-howto.blogspot.com/2010/04/howto-use-named-pipes-to-communicate.html
与现有管道交互的相关链接:http: //v01ver-howto.blogspot.com/2010/04/howto-use-named-pipes-to-communicate.html
回答by jreznot
You can create named pipe using JNA library https://github.com/java-native-access/jna
您可以使用 JNA 库https://github.com/java-native-access/jna创建命名管道
It is clearly shown in the following test: https://github.com/java-native-access/jna/blob/master/contrib/platform/test/com/sun/jna/platform/win32/Kernel32NamedPipeTest.java
它在以下测试中清楚地显示:https: //github.com/java-native-access/jna/blob/master/contrib/platform/test/com/sun/jna/platform/win32/Kernel32NamedPipeTest.java
API of JNA wrapper is the same as Win32 hence you will be able to use all the features and power of named pipes on Windows.
JNA 包装器的 API 与 Win32 相同,因此您将能够在 Windows 上使用命名管道的所有功能和功能。