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

thanks in advance,
nicolas

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.

thanks salem!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.