Please bear with me as I'm new to C. I'm trying to send a struct over a UDP socket. I've looked at send, sendto and sendmsg, but none seem to have this ability. Can it be done?
Both send() and sento() have this ability. The 2nd parameter is an address of message buffer, the 3rd is message length (in bytes). Use (const char*) cast for the structure address - that's all.
There is another problem. UDP protocol has some message length restriction (see YOUR network configuration). If your structure is too long, you must invent your own application level protocol to split and send your structure then to collect full message in the receiver end...
Don't forget: socket API is not part of C standard.
Hi,
Also make sure both ends of your sockets know how to interpret the STRUCT correctly.
For example If one is a 32-bit little endian (Intel x86 based) machine and the other is a 64-bit (Sun Sparc, Intel Itanium ) big endian machine, you might have Byte alignment problems that the structure you send won't match the struture you are receiving.
Hopefully there is a Marshalling Layer to handle the representation differences correctly.
If both ends of your socket are the same type of machine then you don't have to worry about that.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.