send struct over tcp socket in c
c, sockets, struct, tcp
Solution
See here: Passing a structure through Sockets in C. However, in your case, you additionally have pointers inside your structure, so you will have to write contents of those pointers to the destination buffer, too.
Problem
I've got a struct: ``` typedef struct { char *typ; cid_t cid; unsigned short nbytes; char *data; } req_s; typedef struct { char *ip; unsigned short pid; } cid_t; ``` and I want to send it over a tcp socket. Is that possible? I've done it with: ``` req_s answer; ... if( send(sock, (void*)&answer, sizeof(answer),0) < 0 ) { printf("send failed!\n"); } ... recv ( socketDesc, (void*)&answer, sizeof(answer), 0) >= 0) ``` but if I want to read rout my struct answer I only get some hieroglyphs or is there even a better way to send my data from client to server and back?