send struct over tcp socket in c

user1550036

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?

user2793162

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Send C struct over TCP socket

From Dev

Send a struct using socket over http

From Dev

Winsock send () over TCP in C

From Dev

Sending string in a struct over TCP (c++)

From Dev

Serializing a C struct in Python and sending over a socket

From Dev

C socket, send message after file, TCP

From Dev

sending message over a socket using gen_tcp:send/2

From Dev

Send and receive struct in c - socket programming

From Dev

C# Send information over TCP

From Dev

struct char* over tcp

From Dev

Send object over socket

From Dev

Variable length Message over TCP Socket in C++

From Dev

send xml from java to C# via tcp socket

From Dev

How to send a size_t variable with a TCP socket in C?

From Dev

How can I portably send a C struct through a network socket?

From Dev

How to send integer array over a TCP connection in c#

From Dev

How to send html files with http protocol over tcp in c language?

From Dev

Send struct, float and int over UDP sockets in C++

From Dev

Why Socket over Tcp doesn't send if I call it too much in a millisecond?

From Dev

TCP: Socket send/recv order

From Dev

How to send integer array over udp socket in C?

From Dev

How to send float from java to C++ over socket connection?

From Dev

C# send images over socket as fast as possible

From Dev

C# send multiple different objects over 1 socket

From Dev

server can't send message to Client over Socket in C

From Dev

C socket programming sending multiple send and receives over same connection

From Dev

Qt send file over TCP

From Java

Send Object Over Java Socket

From Dev

Qt Stream Audio Over TCP Socket In Realtime