Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

send a struct over a network 3

Status
Not open for further replies.

AndyCLee

Technical User
Mar 10, 2003
49
GB
Hi,

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?

Thanks in advance for any pointers.
 
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.
 
sorry for what are probably stupid questions but.........

If I have a struct called packet, and an instance of it called packetToSend, what's the syntax I use? Is it:

send(s,*packetToSend,sizeof(packetToSend),..........

where s is the socket number.

Thanks for your help

Andy
 
Assuming that your declaration is: struct packet packetToSend

Your call is:

send(s,(const char *)&packetToSend,sizeof(packetToSend),....

If it's declared like this: struct packet *packetToSend

Your call is:

send(s,(const char *)packetToSend,sizeof(*packetToSend),....


Dennis
 
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.
 
Thank you all for your help. It works fine now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top