• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1307
  • Last Modified:

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
1 Solution
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.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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


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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now