Java 和 C/C 之间的命名管道++节目

发布于 2024-10-01 06:21:12 字数 185 浏览 0 评论 0原文

我想在Windows中使用命名管道在用Java和C编写的两个应用程序之间进行通信。通常我使用套接字连接来做到这一点,但现在我必须取消这个想法并寻找新的解决方案。

我读到,java 中的命名管道只能在 JVM 内部可见,这是真的吗? 有没有办法在用不同语言编写的两个应用程序之间建立命名管道?

如果没有,您建议采用哪种技术?

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.

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?

If not, what kind of technology do You advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太傻旳人生 2024-10-08 06:21:13

为了在 Java 中创建 Windows 命名管道,您必须使用 JNI 来调用本机 WINAPI 函数。

不过,您可以在 C++ 中创建命名管道,并在创建后将其作为管道命名空间中的文件打开,从而在 Java 中使用它。

In order to create a Windows named pipe in Java, you'd have to resort to using JNI to call the native WINAPI functions.

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.

韶华倾负 2024-10-08 06:21:13

命名管道比使用套接字更难正确使用。从概念上讲,它们更简单。然而,使它们可靠且具有合理的容错能力比套接字要困难得多。

我建议您重新考虑套接字,这是为进程之间的通信而设计的。你能解释一下为什么不能使用套接字吗?我问的原因是命名管道实际上在环回上使用套接字。

命名管道是操作系统的构造。您可以在操作系统中创建命名管道,然后可以像 Java 和 C 或任何其他程序访问文件一样访问它。通过文件在进程之间进行通信是非常困难的(如果不是不可能的话)例如,您将不知道当您写入命名管道时,任何东西正在读取它,除非您设计自己的流控制协议。 (在所有情况下都很难测试)

您可能听说过 PipedInputStream 和 PipedOutputStream 并且这些类只能在同一个进程中使用(使它们变得毫无用处)

编辑:如果您想要最常见且可能最常见的独立视图发送数据的明智方式我建议你尝试谷歌。

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

因此,套接字可能比命名管道明智 25 倍。 (并且更受支持,因为有更多的教程和有经验的人)

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)

You may have heard of PipedInputStream and PipedOutputStream and these classes can only be used in the same process (making them pretty useless)

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)

软糖 2024-10-08 06:21:13

您可以简单地在 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();

You can simply start an external process in Java and connect to it's pipes.

    // 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文