What is the best way to pass information from java to c++?

Ale*_*lex 18 c++ java sockets cross-platform

I have a java application I need to pass some info to a C++ program. It has been suggested that I use some simple socket programming to do this. Is this the best way? If not what are the alternatives? If so, how should I go about learning about socket programming?

Dav*_*Ray 16

你有几个选择:

  • 将文件从Java传递给C++.这可能是最简单的.它很容易测试,不应该在任何一端都需要任何第三方库.
  • 如上所述使用套接字.在C++中,如果您需要跨平台解决方案,ACEboost等库将为您节省一些心痛
  • 使用JNI从Java调用C++,反之亦然.这可能是最困难的,但性能最高.

对于学习套接字,谷歌搜索"java套接字教程""c ++套接字教程"将为您提供大量信息.


Joh*_*itb 6

一种简单的方法是使用标准输入和输出:

class MyTest {
    public static void main(String... args) {
        System.out.println("carpet");
    }
} // Java

#include <iostream>
#include <string>
int main() {
    string input;
    std::getline(std::cin, input);
    std::cout << "from java: " << input << std::endl; // output: carpet
} // C++

# start by piping java's output to c++'s input
$ java MyTest | ./my_receive 
Run Code Online (Sandbox Code Playgroud)