快速 C++数据到Java传输
我正在尝试以有效的方式将字符串流从 C++ 程序传输到 Java 程序,但我不确定如何执行此操作。任何人都可以发布链接/解释如何实现这一点的基本想法吗?
我正在考虑将数据写入文本文件,然后从 Java 程序读取文本文件,但我不确定这是否足够快。我需要它在 16ms 内传输单个字符串,这样我们就可以在一秒钟内将大约 60 个数据字符串传输到 C++ 程序。
I'm trying to transfer a stream of strings from my C++ program to my Java program in an efficient manner but I'm not sure how to do this. Can anyone post up links/explain the basic idea about how do implement this?
I was thinking of writing my data into a text file and then reading the text file from my Java program but I'm not sure that this will be fast enough. I need it so that a single string can be transferred in 16ms so that we can get around 60 data strings to the C++ program in a second.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需几毫秒即可轻松写入和读取包含 60 个字符串内容的文本文件。
如果您发现无论如何都会遇到计时问题,则可以使用一些替代方案:
使用套接字编程。 http://beej.us/guide/bgnet/output/html/multipage /index.html。
套接字应该足够快。
还有其他替代方案,例如 tibco 消息服务,它比您需要的速度快一个数量级:http:// /www.tibco.com/
另一种替代方法是使用 mysql 表来传递数据,并且可能只设置一个环境变量以指示应查询该表以获取最新条目。
或者我想您可以只使用环境变量本身来传达所有信息——60 个字符串并不是很多。
不过,前两个选项是更受人尊敬的解决方案。
Text files can easily be written to and read from upwards with 60 strings worth of content in merely a few milliseconds.
Some alternatives, if you find that you are running into timing troubles anyway:
Use socket programming. http://beej.us/guide/bgnet/output/html/multipage/index.html.
Sockets should easily be fast enough.
There are other alternatives, such as the tibco messaging service, which will be an order of magnitude faster than what you need: http://www.tibco.com/
Another alternative would be to use a mysql table to pass the data, and potentially just set an environment variable in order to indicate the table should be queried for the most recent entries.
Or I suppose you could just use an environment variable itself to convey all of the info -- 60 strings isn't very much.
The first two options are the more respectable solutions though.
序列化:
protobuf 或 s11n
Serialization:
protobuf or s11n
几乎任何方法都会这么快。文件可能是最慢的,总共可能约为 10 毫秒!。如果您也必须创建一个新连接(它是连接,而不是需要花费大部分时间的数据),那么套接字将是类似的。使用套接字的优点是发送者和接收者知道已生成了多少数据。如果您使用文件,则需要另一种方式来表示,文件现在已完成,您应该阅读它。例如套接字;)
如果 C++ 和 Java 在同一进程中,则可以使用 ByteBuffer 来包装 C 数组并在大约 1 微秒内导入到 Java 中。
Pretty much any way you do this will be this fast. A file is likely to be the slowest and it could be around 10ms total!. A Socket will be similar if you have to create a new connection as well (its the connect, not the data which will take most time) Using a socket has the advantage of the sender and receiver knowing how much data has been produced. If you use a file instead, you need another way to say, the file is complete now, you should read it. e.g. a socket ;)
If the C++ and Java are in the same process, you can use a ByteBuffer to wrap a C array and import into Java in around 1 micro-second.