I’m exchanging data via winsock using sendto and recvfrom functions, but the second param in each is char* buf. I would like to send a custom data like MyStruct*, but I can’t find analogs of those functions to work with void*. I just afraid, sending data in string format and then parsing it might be slow and ineffective. But still, maybe there is a strong reason to force programmers to send data in that way ? Any information about situation and any suggestions are welcomed. Thanks in advance.
I’m exchanging data via winsock using sendto and recvfrom functions, but the second param
Share
If your
structdoes not contain any pointers or handles and the receiver is written by you using the same compiler and compile flags, and you do not care about portability (e.g. big-endian architectures), then you can just cast yourMyStruct*tochar*and it will work.In ever other case, you’ll die a horrible death. No, seriously. It won’t work. Pointers and handles are not meaningful on another machine, and using a different compiler or compilation flags may result in the
structhaving a slightly different layout or padding, rendering it useless.Sending structures via the network usually, with very few exceptions, requires serialization.