Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send struct over tcp socket in c [duplicate]

Tags:

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?


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!