简体   繁体   English

Java和C / C ++程序之间的命名管道

[英]Named pipes between Java and C/C++ programs

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? 我读到Java中的命名管道仅在JVM内部可见,这是真的吗? Is there a way to establish named pipe between two apps wriiten in different language? 有没有办法在两个使用不同语言的应用程序之间建立命名管道?

If not, what kind of technology do You advice? 如果没有,您建议使用哪种技术?

In order to create a 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中通过在创建命名管道之后将其作为文件在命名空间中打开来使用它。

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. 命名管道是OS构造。 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. 您可以在操作系统中创建命名管道,然后可以像Java和C或任何其他程序将其作为文件一样对其进行访问。 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) (在所有情况下都很难测试)

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. 编辑:如果您想要最常见和可能最明智的方式来发送数据的独立视图,我建议您尝试使用google。

java sockets - 2,210,000 hits
java named pipes - 90,000 hits

So perhaps sockets is 25x more sensible than named pipes. 因此,也许套接字比命名管道明智得多25倍。 (and more supportable as there more tutorials and people who have experience with them) (随着更多的教程和有经验的人的加入,它也将受到更多的支持)

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM