Java 和 C/C++ 程序之间的命名管道
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4112480/
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
Named pipes between Java and C/C++ programs
提问by lipkerson
I think of using in windows a named pipe to communicate between two apps written in Java and C. Normally i use socket connection to do this, but now i have to cancel this idea and find a new solution.
我想在 Windows 中使用命名管道在两个用 Java 和 C 编写的应用程序之间进行通信。通常我使用套接字连接来做到这一点,但现在我不得不取消这个想法并找到一个新的解决方案。
I read that named pipe in java can be visible only inside JVM-is this true? Is there a way to establish named pipe between two apps wriiten in different language?
我读到 java 中的命名管道只能在 JVM 中可见 - 这是真的吗?有没有办法在两个用不同语言编写的应用程序之间建立命名管道?
If not, what kind of technology do You advice?
如果没有,您建议采用哪种技术?
回答by Jim Brissom
In order to createa Windows named pipe in Java, you'd have to resort to using JNI to call the native WINAPI functions.
为了在 Java 中创建Windows 命名管道,您必须求助于使用 JNI 来调用本机 WINAPI 函数。
You can create the named pipe in C++, though, and consume it in Java by opening it as a file in the pipe namespace after it has been created.
不过,您可以在 C++ 中创建命名管道,并在创建后将其作为管道命名空间中的文件打开,从而在 Java 中使用它。
回答by Peter Lawrey
Named pipes are much harder to get right than using sockets. Conceptually they are simpler. However making them reliable and reasonably fault tolerant is much harder than for sockets.
命名管道比使用套接字更难正确使用。从概念上讲,它们更简单。然而,使它们可靠和合理的容错比套接字要困难得多。
I would suggest you reconsider sockets, this is designed for communication between processes. Can you clarify why you cannot use sockets? The reason I ask is that named pipes actually used sockets over loopback in reality.
我建议您重新考虑套接字,这是为进程之间的通信而设计的。你能解释一下为什么不能使用套接字吗?我问的原因是命名管道实际上在环回上实际上使用了套接字。
A named pipe is an OS construct. You can create the named pipe in your OS and then it can be accessed as if it were a file by Java and C, or any other program. Communication between processes via file is very difficult to get right (if not impossible) For example, you will have no idea that when you write to the named pipe, that anything is reading it unless you design your own flow control protocol. (Very difficult to test in all cases)
命名管道是一种操作系统构造。你可以在你的操作系统中创建命名管道,然后它可以被 Java 和 C 或任何其他程序访问,就像它是一个文件一样。通过文件进行进程之间的通信非常困难(如果不是不可能的话)例如,除非您设计自己的流量控制协议,否则您将不知道当您写入命名管道时,任何东西都在读取它。(在所有情况下都很难测试)
You may have heard of PipedInputStream and PipedOutputStream and these classes can only be used in the same process (making them pretty useless)
您可能听说过 PipedInputStream 和 PipedOutputStream 并且这些类只能在同一进程中使用(使它们变得毫无用处)
EDIT: If you want an independant view of the most common and possibly the most sensible way to send data I suggest you try google.
编辑:如果您想独立了解最常见且可能是最明智的数据发送方式,我建议您尝试谷歌。
java sockets - 2,210,000 hits
java named pipes - 90,000 hits
So perhaps sockets is 25x more sensible than named pipes. (and more supportable as there more tutorials and people who have experience with them)
所以套接字可能比命名管道更明智 25 倍。(并且更受支持,因为有更多的教程和有经验的人)
回答by Peter Knego
You can simply start an external process in Java and connect to it's pipes.
您可以简单地在 Java 中启动一个外部进程并连接到它的管道。
// Execute command
String command = "ls";
Process child = Runtime.getRuntime().exec(command);
// Get pipes from process
InputStream in = child.getInputStream();
OutputStream out = child.getOutputStream();
InputStream error = child.getErrorStream();