Well, to answer the question asked ("How to send structs?"), You simply send a pointer to the structure.
struct foo { int a; int b; }; struct foo s; sa = 10; sb = 20; send(socket,&s,sizeof(s),0);
It's really that simple. Now the other side should be the same (i.e., the structure laid out in memory in system A should correspond to the structure set out in system B). It is best to use more specific types and some functions to properly organize the data:
#ifndef __GNUC__ # define __attribute__(x) #endif #ifdef __SunOS # pragma pack(1) #endif struct foo { uint32_t a; uint32_t b; } __attribute__((packed)); #ifdef __SunOS # pragma pack() #endif struct foo s sa = htonl(10); sb = htonl(20); send(socket,&s,sizeof(s),0); recv(socket,&s,sizeof(s),0); sa = ntohl(sa); sb = ntohl(sb);
You can see that it begins to quickly get system-specific when you want to “transfer” binary data over the network, so people say that they convert to text or, quite possibly, use the already written serialization library.
source share