简体   繁体   English

C ++客户端和Java Server之间的通信

[英]Commucation Between C++ Client and Java Server

I'm developing a Client application which talks to a Server using HTTP. 我正在开发一个客户端应用程序,它使用HTTP与服务器通信。 The Client is in C++ and the Server component is in J2EE. 客户端使用C ++,Server组件使用J2EE。 I'm able to exchange data between Client and Server successfully. 我能够成功地在客户端和服务器之间交换数据。 Now I want to encapsulate pices of information in a C++ object in client and send it in the HTTP request to server. 现在我想在客户端的C ++对象中封装信息,并将其发送到服务器的HTTP请求中。 My server after receiving the data should deserialize the C++ object to a java object. 接收数据后我的服务器应该将C ++对象反序列化为java对象。

My question is, I want to know whether we can exchange an object in the above mentioned way and if it can be achieved, could someone help me by providing references to online tutorials or code sample? 我的问题是,我想知道我们是否可以用上面提到的方式交换对象,如果可以实现,有人可以通过提供在线教程或代码示例的参考来帮助我吗?

You need to serialize the object into XML, JSON or something like that. 您需要将对象序列化为XML,JSON或类似的东西。

You can't use technology-specific serialization (eg RMI for java) because you are working cross stack (and it's bad practice anyway). 您不能使用特定于技术的序列化(例如,用于Java的RMI),因为您正在使用跨堆栈(并且无论如何都是不好的做法)。

Don't know about c++ but in java binding objects to XML/JSON is trivial using JAXB. 不了解c ++,但在java中,使用JAXB将对象绑定到XML / JSON是微不足道的。 If you are exposing a webservice you can go the easy way: REST (using JAX-RS) or the slightly harder but technically better way (in that it has more features): SOAP (using JAX-WS). 如果您正在公开Web服务,您可以采用简单的方法:REST(使用JAX-RS)或稍微更难但技术上更好的方式(因为它具有更多功能):SOAP(使用JAX-WS)。

Exposing such webservices is trivial in Java EE if you read up on the relevant webservice stacks. 如果您阅读相关的Web服务堆栈,那么在Java EE中公开这样的Web服务是微不足道的。

It would be a nightmare if you send plain old C++ objects to the Server. 如果将普通的旧C ++对象发送到服务器,那将是一场噩梦。 You need to have a common protocol between client and server. 您需要在客户端和服务器之间建立通用协议。 A good option for this is JSON. 一个很好的选择是JSON。 The flow would be like this: 流程将是这样的:

C++ object -> serialize to -> JSON -> deserialize to -> Java object C ++对象 - >序列化为 - > JSON - >反序列化为 - > Java对象

To serialize C++ objects into JSON using something like JSON Spirit . 使用JSON Spirit之类的东西将C ++对象序列化为JSON。 To deserialize the JSON into Java objects, Gson is a safe bet (it even does the conversion on-the-fly). 要将JSON反序列化为Java对象, Gson是一个安全的赌注(它甚至可以即时进行转换)。 You can find documentation for both projects on their respective homepages. 您可以在各自的主页上找到这两个项目的文档。

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

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