I have a java program that will send(over TCP) packets to a c++ program. Only two types of objects will be sent (struct A or struct B). I need to serialize the two objects and send them to the c++ program, such that when de-serialized inside the c++ program they will be of the type struct A or struct B, depending on what has been sent. How can I achieve this without any external java packages?
struct A
{
unsigned int field1;
unsigned int field2;
}
struct B
{
unsigned int length;
struct A list[GLOBALLENGTH];
}
GLOBALLENGTH is a static and globel int.
Please note that I dont need a general solution, as I will strictly be dealing with the mentioned types above. Also, I dont have access to the c++ program(it is a blackbox), both operate in little indian.
It sounds like the c++ program expects a raw dump of the data in each structure, with no padding, in little-endian order, with the fields in the declared order, using 32-bit integers. If all that’s right, here’s one way to write bytes for consumption by the c++ program:
You can then call the appropriate public method (
writeA(A)orwriteB(B)) and send the bytes to the server.