简体   繁体   English

将数据从C传递到Java

[英]Passing data from C to Java

What is the best way to pass data between a C server to a Java applet? 在C服务器和Java小程序之间传递数据的最佳方法是什么? Currently it's done by using a buffer and copying bytes to it using memcpy, but if the object ever changes I have to hunt down every instance of where it's being sent to the applet and have it changed. 当前,这是通过使用缓冲区并使用memcpy将字节复制到该缓冲区来完成的,但是如果对象发生更改,我就必须查明将其发送到applet的每个实例并进行更改。

Is there a better way to pass object to java? 有没有更好的方法将对象传递给Java? How would you do it? 你会怎么做?

Edit for clarification: It is going from the C server to the Java applet only, there is no java servlet. 编辑以进行澄清:仅从C服务器到Java小程序,没有Java servlet。 The only thing the Applet passes to the C server is a hex number signaling an action to be taken Applet传递给C服务器的唯一内容是一个十六进制数字,表示要采取的措施

You might want to take a look at Protocol Buffers 您可能要看一下协议缓冲区

They let you define structured data, then generate the code to read and write messages in any of the supported languages. 它们使您可以定义结构化数据,然后生成代码以使用任何受支持的语言读取和写入消息。 The only catch is I think they only support C++ for now, though I know there was interest in straight C implementation. 唯一要注意的是,我认为他们目前仅支持C ++,尽管我知道对直接C实现很感兴趣。

I would have opened a socket connection between the two applications. 我会打开两个应用程序之间的套接字连接。 You could serialize the object to an array of bytes and have the Java servlet deserialize it into a Java object with the same value. 您可以将对象序列化为字节数组,并使Java servlet将其反序列化为具有相同值的Java对象。 This way if the class definition changes, you only have to change the code in two places. 这样,如果类定义更改,则只需在两个位置更改代码。

I would serialize the objects to either XML or google protobuf on the C side and have them deserialized on the applet side using a single deserializer. 我将在C端将对象序列化为XML或google protobuf,并使用单个反序列化器在applet端对它们进行反序列化。 Ie don't have more then one class that does the deserialization. 即,只有一个以上的类可以进行反序列化。 Make user that you version the serialization and that the object that does the de-serialization throws an exception if the version it tries to read is not backward compatible. 如果用户尝试读取的版本不向后兼容,则使用户对版本进行序列化,并且进行反序列化的对象将引发异常。 It would be a bad practice to have the serialization and deserialization all over the code. 对整个代码进行序列化和反序列化将是一个不好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM