简体   繁体   English

Java和C应用程序之间的IPC

[英]IPC between Java and C applications

I have 2 applications, one written in C and the other in Java, that suppose to run on the same machine. 我有2个应用程序,一个用C语言编写,另一个用Java编写,假设在同一台机器上运行。 What is the best way to implement an IPC between them? 在它们之间实施IPC的最佳方法是什么?

best meaning portability, minimal user awareness (firewall popups...), etc. 最佳意义的可移植性,最小的用户意识(防火墙弹出窗口......)等。

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. 延迟大约为20-50微秒,具体取决于您对数据执行的操作以及发送的数量。

Sockets are platform independent and can be use in pure Java. 套接字是独立于平台的,可以在纯Java中使用。 There are millions of examples and tutorials available for Java. 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. 我发现最简单的方法是从java执行本机程序,然后通过进程输入和输出流进行通信。 This only works for ASCII communication though, which in most cases is fine. 这仅适用于ASCII通信,在大多数情况下这很好。 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. 总有JNI或JNA,但这些通常最适合java和本机代码之间的紧密联系。

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. 我从来没有尝试过,但Java确实支持内存映射文件http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/MappedByteBuffer.html这将使您能够共享对象C和Java应用程序之间。 Synchronizing access might be challenging because I don't think Java has direct access to OS-level IPC synchronization primitives. 同步访问可能具有挑战性,因为我不认为Java可以直接访问操作系统级别的IPC同步原语。

您可以使用命名套接字或网络套接字来实现此目的。

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

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