English 中文(简体)
What are the possible ways to exchange data in binary format between windows and solaris?
原标题:

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

问题回答

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

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.

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.

Boost serialization seems like a good choice.

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签