views:

111

answers:

2

Hi, friends.

I have a general socket programming question for you.

I have a C struct called Data:

struct data {
      double speed;
      double length; 
      char carName[32];
      struct Attribs;
}

struct Attribs {
      int color;
}

I would like to be able to create a similar structure in Java, create a socket, create the data packet with the above struct, and send it to a C++ socket listener.

What can you tell me about having serialized data (basically, the 1's and 0's that are transferred in the packet). How does C++ "read" these packets and recreate the struct? How are structs like this stored in the packet?

Generally, anything you can tell me to give me ideas on how to solve such a matter.

Thanks!

A: 

The basic process is:

  • java app creates some portable version of the structs in the java app, for example XML
  • java app sends XML to C++ app via a socket
  • C++ app receives XML from java app
  • C++ app creates instances of structs using the data in the XML message
anon
I have been told to use an XML Schema file (XSD). And then use ant to create a Java Bean class. The java bean class is serializable... so I basically send the data from the java bean class. Is that the correct method? Going into the specifics, how can one have an XML analog for char arrays, or enumerations?Btw, where can I read more of this?
Carlo del Mundo
@Carlo You can't have direct analogs for all Java and C++ types - your code will have to perform suitable conversions. So create your bean, have it serialize stuff, and then look at the XML it produces. Alternatively, find a library that will do all this stuff for you or use (shudder) something like CORBA, SOAP etc.
anon
Thanks sir! I'll keep this in mind!
Carlo del Mundo
+2  A: 
  • Be weary of endianness if you use binary serialization. Sun's JVM is Big Endian, and if you are on an Intel x86 you are on a little endian machine.
  • I would use Java's ByteBuffer for fast native serialization. ByteBuffers are part of the NIO library, thus supposedly higher performance than the ol' DataInput/OutputStreams.
  • Be especially weary of serializing floats! As suggested above, its safer to transfer all your data to character strings across the wire.
  • On the C++ side, regardless of the the networking, you will have a filled buffer of data at some point. Thus your deserialization code will look something like:

size_t amount_read = 0;
data my_data;
memcpy(buffer+amount_read, &my_data.speed, sizeof(my_data.speed))
amount_read += sizeof(my_data.speed)
memcpy(buffer+amount_read, &my_data.length, sizeof(my_data.length))
amount_read += sizeof(my_data.length)
  • Note that the sizes of basic C++ types is implementation defined, so you primitive types in Java and C++ don't directly translate.
  • You could use Google Protocol buffers. My preferred solution if dealing with a variety of data structures.
  • You could use JSON for serialization too.
imran.fanaswala
Thanks! I'll look into this!
Carlo del Mundo