How to send and recieve struct over udp or tcp-ip

How to send struct over Udp or Tcp-Ip Socket and How to recieve struct.Using borland c++
Builder 3 (using Udp or Tcp-Ip in Inernet Tools). And I want Example with comment
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

net3phoneAuthor Commented:
Edited text of question.
just treat the struct as a buffer.  Use casts and the sizeof operator.
What about padding issues?  If it is send to a different platrform the structure may be laid out differently.  You may want to control the padding to prevent those sorts of problems.  Different compilers have different options for that.  Builder uses "#pragma pack" to control that.  You also have to make sure that the members inside the structure don't change size betweent eh two different platforms.   Also if  the structure contains pointers do additiona data, the pointer will be sent this way, which is useles, and the additional data will not be.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

in that case, you also want to handle endianness (use htonl(), htons(), ntohl() and ntohs() functions) but this can be done after receiving the buffer.
If ur code does not need efficiecy then you can simple use 0 byte packing for the strunctures and send it as a character array.

#pragma pack(0) // Note for MSVC compile other OS/compilers give this support using different pragms , check for equivalent in BC
struct a
   int b;
   int c;
   char s;
   float f;
#pragma pack()

This will force that sizeof struct will be size of the sum of member.

Now The sender will do like this :

s obj;

send(s,(char*) &obj, sizeof(obj), ..);

Receieve will receive like this :

s obj
recv( s, (char*)&obj, sizeof(obj), ..)

Note : If you are sending with same OS on both side ( as well as same compiler) then you may not have to use pack pragma.

I have send structures across Windows 3.1 , NT, Unix(IRIX) VMS , OS9


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

2.15 How do I send [this] over a socket?

Anything other than single bytes of data will probably get mangled unless you take care. For
integer values you can use htons() and friends, and strings are really just a bunch of single
bytes, so those should be OK. Be careful not to send a pointer to a string though, since the
pointer will be meaningless on another machine. If you need to send a struct, you should write
sendthisstruct() and readthisstruct() functions for it that do all the work of taking the
structure apart on one side, and putting it back together on the other. If you need to send floats,
you may have a lot of work ahead of you. You should read RFC 1014 which is about portable
ways of getting data from one machine to another (thanks to Andrew Gabriel for pointing this
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.