Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Java data types to C/C++ 4

Status
Not open for further replies.

Zech

Programmer
Oct 14, 2002
116
US

Hi,

I have a server program that is going to be written in C/C++ and some clients written in Java. The client programs are going to send and receive binary data from the server. Now my problem is with interpreting Java binaries to something that C/C++ can understand.

I understand that Java data types are somewhat longer than C/C++ data types and that Java data types may require further interpretation by JVM.

I wonder whether anyone has done this before and whether anyone has a good solution to this problem. Thanks.

Zech
 
Avoid binary data exchange where possible. Use the simplest (or more sofisticated) text exchange formats/protocols...
You can evade many troubles with the simplest text converters on both sides.
It seems the main trouble with basic data types is Java long (64 bits)...
 
Why not use Corba - most of the server side stuff is in Java and the rest of it is whatever you want. Lots of Corba engines about.
 
Thanks for all the replies.

Salem posted an interesting article there. Unfortunately, it takes too much trouble to implement it. :p

ArkM brought up another good solution. However, it may not be feasible to my situation. The reason why the transmission has to be in binary is because the data being transmitted is encrypted. I am hoping that I don't have to mess with both the existing Java and C/C++ clients implementation. My plan is to do minimal work with the C/C++ server. The Java clients currently have its own Java server, which essentially performs the same functions as its C/C++ server counterpart. Now, my intention is to scrap the Java server and let the C/C++ server handles both the C/C++ and Java clients.

Using Corba is another possible solution. I still have to look into that.
 
OK, but encrypted data stream and its contents are different things. If you want to process Java types in C++ then your server deciphers it before...
Of course, I don't insist on having it one's own way. Some years ago we have a lot of headaches in Java/C++ heterogenious communications project: byte order, data type sizes, string representations etc...
 
The Java client has:

Code:
// Java code

class SuperThing
{
    public int id;
    public String name;
}

The C++ server has:

Code:
class SuperThing
{
  public:
    int id;
    std::string name;
};



The Java client has a SuperThing object [tt]thing[/tt] that has id 20 and name Foo. It wants to send this object to the server asking it to add it to the SuperThing database.


It writes:
Code:
ADD SuperThing 20 Foo


Now it encrypts that:

Code:
na8f70A&*)HUD093qja


Now it can send that.


Now the server sees:

Code:
na8f70A&*)HUD093qja


It decrypts it:

Code:
ADD SuperThing 20 Foo


It interprets it, creates an appropriate C++ object, and performs the desired action.


For more complicated objects, you wouldn't usually send each data member; you'd just send something representing the state of the object and let the server decide what the data members should from that. After all, they might be using different data members in the first place.


Of course, it helps if your application has a simple, well-defined protocol in the first place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top