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.
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.
You can use either named sockets or network sockets for this purpose..
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.
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.
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.