views:

141

answers:

5

Hi Everyone,

Could someone please help and tell me if there is any possible way to pass a data structure (i.e. binary format) through internet sockets between a program running on Windows and other program running on Unix?

Any idea or link to materials that deal with it would be very appreciated. Thanking you in advance for your help, Mk

+5  A: 

Check out Google's protocol buffers as a fast way of serializing data.

Rob Walker
thanks for your answer, the way you mentioned exchange data in the form of ascii strings and this is different from binary format (i.e., in the form of struct). Thanks again!
make
Actually, protocol buffers is __a__ binary format. The "i.e. in the form of a struct" tells me you think there is only one binary format. That's untrue.
MSalters
+3  A: 

Yes, what you are looking for is some sort of serialization. Many ways of doing this exist, including:

Typically, you would use a library to assist you in this. You may also encounter tools that will generate code for you to link into your program.

Adam Goode
thanks for your answer, the ways you mentioned exchange data in form of ascii strings and those are different from binary format (i.e., in the form of struct). Thanks again
make
Could you please tell me where I can find more information desribing on how to eachange XML messages between programs written in C/C++? Thanks for your help.
make
You can definitely pass binary bits from one OS to another via a socket. If you are willing to reinterpret them on the other side, which is a moderate amount of work to get right. The formats Adam and Rob are mentioning save you this work: they take structure as you describe it on the way out and build it back on the way in. I'd take a good luck at protocol buffers and the rest before deciding against them.
quark
Thanks for your reply and help. Yes! you are right it is hard to work with the "struct" way, but I already started working it sometimes ago and I want to finish it. sorry! I didn't know about protocol buffers and now when I looked on the web to it, it sounds this is what I look for. Just a question, does protocol buffers deal with float and double variables ? thanks again for your help!
make
A: 

Boost serialization seems like a good choice.

Brian R. Bondy
thanks for your answer, the way you mentioned exchange data in the form of ascii strings and this is different from binary format (i.e., in the form of struct). Thanks again!
make
A: 

Although a binary format was requested, if you ever want to debug this kind of thing, it's really helpful to have a textual format that you can read as a human, so the suggestions of XML and maybe JSON might be appropriate. Most systems and languages have handy free libraries for reading and writing both of those, and in many, XML is built in. This also tends to mean they are well tested, integrated and performant.

dajobe
thanks for your answer, the suggestion of XML exchanges data in form of strings and this is different from binary format (i.e., in the form of struct). Thanks again
make
Could you please tell me where I can find more information desribing on how to eachange XML messages between programs written in C/C++? Thanks for your help.
make
+4  A: 

If you are against the text serialization and really want a struct, then do it like most network protocols do with "host to network" when sending and "network to host" when receiving for all fields within the struct. The idea is that all senders no matter what endianness they are always translate to network (big-endian I forget which is which). Then all receivers translate to whatever they are (might also be big-endian which is no change).

There are apis already for this. They are ntohs (network to host short for 16-bit fields) and ntohl (32-bit fields). Then of course htons and htonl.

So for a struct like this:

typedef struct
{
    unsigned char  stuff1;
    unsigned char  stuff2;
    unsigned short stuff3;
    unsigned int   stuff4;
}tFoo;

The sending code would do something like:

tFoo to_send;
to_send.stuff1 = local_stuff1;
to_send.stuff2 = local_stuff2;
to_send.stuff3 = htons(local_stuff3);
to_send.stuff4 = htonl(local_stuff4);

The receiving code would do something like:

local_stuff3 = ntohs(from.stuff3);
local_stuff4 = ntohl(from.stuff4);

Note that packing/alignment matters of the struct. You have to be weary of alignment which isn't always the same from compiler to compiler and even for the same compiler on different cpu architectures. It can even change for the datatypes themselves (an int may not be the same size from arch to arch). I attempted to demonstrate that a bit with the first two 8-bit chars, followed by a 16-bit and a 32-bit for a total of 8 bytes. You have to be certain that when you port to a different compiler/arch that you are indeed getting the correct packing/size.

For that reason most people choose serialization and probably why most people of answered with that. It is the least error prone.

Good luck.

tim
thanks for your answer. just a question, when a struct contains a float and/or double varibale, how can we manage it with the way you described? thanks again!
make
A float is 32-bit so you should be able to convert to int, htonl, then back to a float. This of course assumes that the float representation is the same between the source and target architectures.Here is reference linkhttp://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers
tim