hi, i was wondering if it is possible to send a struct through sockets...

thanks in advance,

9 Years
Discussion Span
Last Post by n.aggel

Yes, you can.
No, it's a bad idea to try.

It's a bad idea because the packing and alignment of the data is dependent on the compiler, and the endian-ness of the data is dependent on your architecture. If there is any variance between the sender and receiver, the receiver could have a hard time understanding what the hell you're talking about.

If your struct contains pointers, then just forget about sending them "as is", because the pointer really won't mean a damn thing to the other machine.


thanks for your answer salem,

so to do this properly i should send the componenets of the struct one by one?

PS:: i didn't mention on my first post that the communication will be in the same machine{by a "client" and a "server" application}


Ideally, you should make the external representation (whether across a network or saved in a file) as implementation neutral as possible, to give the widest possible chance for something else to pick it up and use it.

If you're constraining yourself to the same machine, then it's a judgement call on your part. But at least hopefully you'll be making an informed choice.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.