IPC between Java and C applications

2020-04-11 02:48发布

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.

标签: java c
4条回答
仙女界的扛把子
2楼-- · 2020-04-11 03:33

You can use either named sockets or network sockets for this purpose..

查看更多
▲ chillily
3楼-- · 2020-04-11 03:36

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.

查看更多
手持菜刀,她持情操
4楼-- · 2020-04-11 03:39

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.

查看更多
三岁会撩人
5楼-- · 2020-04-11 03:47

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.

查看更多
登录 后发表回答