Java 和 C++ 之间 IPC 的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/165945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best approach for IPC between Java and C++?
提问by
I would like to implement a robust IPC solution between a single JVM app (one process, potentially multiple threads) and a native C++ application that is linked to a C++ dll. The dll may or may not be on the same physical machine. What is the best approach for doing so?
我想在单个 JVM 应用程序(一个进程,可能是多个线程)和链接到 C++ dll 的本机 C++ 应用程序之间实现一个强大的 IPC 解决方案。dll 可能位于也可能不在同一台物理机器上。这样做的最佳方法是什么?
Any suggestions will be greatly appreciated! Thanks!
任何建议将不胜感激!谢谢!
回答by Vinko Vrsalovic
I'd use a standard TCP/IP socket, where the app listens on some port and the library connects to it to report what it has to report and expect the answers.
我会使用标准的 TCP/IP 套接字,其中应用程序侦听某个端口,库连接到它以报告它必须报告的内容并期待答案。
The abstraction is robust, well supported and will have no interop issues.
抽象是健壮的,得到很好的支持,不会有互操作问题。
回答by Swaroop C H
Have you considered Facebook's Thrift framework?
你考虑过Facebook 的 Thrift 框架吗?
Thrift is a software framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.
Thrift allows you to define data types and service interfaces in a simple definition file. Taking that file as input, the compiler generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages.
Thrift 是一个用于可扩展的跨语言服务开发的软件框架。它结合了软件堆栈和代码生成引擎,以构建在 C++、Java、Python、PHP、Ruby、Erlang、Perl、Haskell、C#、Cocoa、Smalltalk 和 OCaml 之间高效无缝工作的服务。
Thrift 允许您在一个简单的定义文件中定义数据类型和服务接口。以该文件作为输入,编译器生成用于轻松构建跨编程语言无缝通信的 RPC 客户端和服务器的代码。
It can work over TCP sockets and the serialization/deserialization is already built-in.
它可以在 TCP 套接字上工作,并且序列化/反序列化已经内置。
Read the whitepaperfor details.
阅读白皮书了解详情。
回答by Chris de Vries
Google protocol buffercan help you serialize data in a language and platform neutral way. It will also generate code in Java and C++ to handle reading and writing the serialized data. You can then use any communication mechanism you wish to send the data. For example, you could send it over a TCP socket or via shared memory IPC.
Google 协议缓冲区可以帮助您以一种语言和平台中立的方式序列化数据。它还将生成 Java 和 C++ 代码来处理读取和写入序列化数据。然后,您可以使用您希望发送数据的任何通信机制。例如,您可以通过 TCP 套接字或共享内存 IPC 发送它。
回答by Chris de Vries
I am looking at the Remote Call Framework, for my purely C++ apps. The specs look pretty and promising. I am going to try it.
我正在查看Remote Call Framework,用于我的纯 C++ 应用程序。规格看起来很漂亮,很有前途。我要试试。
回答by Kevin Day
mmm - DLLs are not processes, so I'm assuming you mean IPC between your Java app, and some other native application that is linked to the DLL. Sockets, for certain, are the way to go here. It will make everything easier for you.
mmm - DLL 不是进程,所以我假设您的意思是 Java 应用程序与链接到 DLL 的其他一些本机应用程序之间的 IPC。当然,套接字是通往这里的方式。它会让你的一切变得更容易。
Another option would be to use JNI to talk to a DCOM implementation, but I don't think you'll gain much (other than having to deal with the headaches of COM and JNI :-) ).
另一种选择是使用 JNI 与 DCOM 实现对话,但我认为您不会获得太多收益(除了必须处理 COM 和 JNI 的头疼问题 :-) )。

