Java 和 C 应用程序之间的 IPC
我有 2 个应用程序,一个用 C 编写,另一个用 Java 编写,假设在同一台机器上运行。在它们之间实现 IPC 的最佳方式是什么?
最好的意义是可移植性、最小的用户意识(防火墙弹出窗口...)等。
I have 2 applications, one written in C and the other in Java, that suppose to run on the same machine. What is the best way to implement an IPC between them?
best meaning portability, minimal user awareness (firewall popups...), etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会在循环中使用套接字来开始。这允许您发送文本或二进制数据,并在一个进程启动或终止时干净地处理。延迟约为 20-50 微秒,具体取决于您对数据的处理方式以及发送的量。
套接字与平台无关,可以在纯 Java 中使用。有数百万个 Java 示例和教程。
I would use Sockets over loop back to start with. This allows you to send text or binary data and cleanly handle when one process starts or dies. The latency is about 20-50 micro-seconds depending on what you are doing with the data and how much you send.
Sockets are platform independent and can be use in pure Java. There are millions of examples and tutorials available for Java.
我发现最简单的方法是从 java 执行本机程序,然后通过进程输入和输出流进行通信。虽然这只适用于 ASCII 通信,但在大多数情况下都很好。这种方法适用于跨平台。如果应用程序彼此独立启动,则文件或套接字都是跨平台方法,并且可以在需要时使用二进制数据。
总有 JNI 或 JNA,但它们通常最适合 java 和本机代码之间的紧密耦合。
I've found that the simplest approach is to exec the native program from java, and then communicate via the process input and output streams. This only works for ASCII communication though, which in most cases is fine. This approach works across platforms. If the applications are launched independently of eachother, then files or sockets are both cross-platform approaches and will work with binary data if desired.
There's always JNI or JNA, but these are typically best suited to a tight couplin between java and native code.
为此,您可以使用命名套接字或网络套接字。
You can use either named sockets or network sockets for this purpose..
我从未尝试过,但Java确实支持内存映射文件 http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/MappedByteBuffer.html
这将使您能够在 C 和 Java 应用程序之间共享对象。同步访问可能具有挑战性,因为我认为 Java 无法直接访问操作系统级 IPC 同步原语。
I have never tried it, but Java does have support for memory mapped files http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/MappedByteBuffer.html
which would give you ability to share objects between C and Java apps. Synchronizing access might be challenging because I don't think Java has direct access to OS-level IPC synchronization primitives.