This question isn't entirely clear to me, but I see three coherent parts:
How Do I Ensure that an Object is Packed in C++?
std::alignas
If your compiler supports it, use std::alignas. Some don't (GCC 4.8 comes to mind).
__attribute__((packed))
With GCC, you can use the compiler-specific attribute "packed".
#pragma pack(1)
I don't recommend this one, but if you're using VC++ or some GCC versions, you can use a pragma to ensure a struct is packed. c.f. this post.
Use a serialization library
There are plenty. I've heard good things about cereal.
How do I send an object over TCP in C++?
Let's assume you can open a TCP socket and establish a TCP connection. Then, if you're on POSIX, you can use sendto. Just hand it a pointer to your struct
, and the sizeof()
that struct
, and you're good to go.
How do I Deserialize a Packed Object in C++?
If you're interested in deserialization in C++, you'll want recvfrom.
If you're interested in serialization in C#, I'll let someone with more expertise answer.