What is the best way to get a byte array from a struct to send over TCP sockets? I’m using .Net (VB or C#).
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
One option is to marshal the native representation of the struct into a buffer directly, similar to how
memcpyworks in C.You would need to add the appropriate attribute to your struct,
Then you can serialize it using:
Still, this is by far the least portable solution. Both sides will need to use the same alignment and endianness, and you will need to implement versioning yourself if you will ever need to change the struct itself.
In most cases, your serialization format shouldn’t be a direct copy of your internal data structures.