KaiserJohaan
KaiserJohaan

Reputation: 9240

Sending C# struct to C++ through a socket

I want to send a struct in C# to C++ using sockets.

For example, I use this struct:

[StructLayout(LayoutKind.sequential, Pack = 1)]
struct pos {
    public int i;
    public float x;
};

If I somehow convert it into bytes and send over the network, I should be able to cast it to this in c++:

struct pos {
    int i;
    float x;
};

... I think.

1) how do you break down a struct instance in C# to send it over the network?

2) can I safely cast it to the c++ struct once I get it?

Thanks

Upvotes: 1

Views: 2982

Answers (2)

OregonGhost
OregonGhost

Reputation: 23759

The marshaller helps you with converting between .NET structs and raw bytes. In this answer, I posted a simple solution, which boils down to Marshal.StructureToPtr and Marshal.PtrToStructure. In contrast to the more advanced solutions provided by Johann du Toit, this is in my opinion the best thing you can do if all you want to do is to push some structures through a byte stream.

If you do this, you can safely cast to the C++ struct if the length is correct, and your C++ struct is declared with the same packing as the C# struct (i.e. #pragma pack in VC++ or __attribute__((packed)) in GCC).

Note that this also works with fixed length C strings, but will not take care of the endianness of larger values. I found it a simple solution to provide getters and setters for the latter problem which just swap the bytes accordingly (with BitConverter).


Some elaboration on the packing: Take the following structure:

struct MyStruct {
    uint8_t a;
    float b;
};

With the C# declaration with StructLayout, Pack = 1, this struct will have a size of five bytes. The C++ struct, however, may have eight bytes (or even more), depending on the default packing of the compiler, who may happily insert some padding bytes to align the float value on a 32-bit boundary (just an example). Because of this, you have to apply the very same packing to both the C# and C++ struct. In Visual C++:

#pragma pack(push, 1)
// ... struct declarations...
#pragma pack(pop)

This means all structs declared between the two pragmas will have a packing of one. In GCC:

struct x {
// ...
} __attribute__((packed));

This will do the same. You can #define __attribute__(x) on Windows platforms and #ifdef _WIN32 around the pragmas to make the code compatible with both worlds.

Upvotes: 5

Johann du Toit
Johann du Toit

Reputation: 2667

You can either encode it in a format like JSON (There are a lot of JSON parsers out there, check on the json.org website for a list), XML or just roll your own. You could also try already built libraries like Protobuf, which allows you to serialize your structures that you would create with a file in .proto format (And use Protobuf-Net for C#). Another option would be Thrift which provides a way to serialize but also supplies a ready to use RCP system. It support c#, c++ and a ton of other languages by default.

So it's depends on taste, take your pick :D

Upvotes: 1

Related Questions