Sending a struct over TCP

J

Jack

Hi all,
I have a problem. I am writing a client server app in C. In my client
program, I have a simlple struct similiar to:

struct mySt{
int id;
char val;
struct mySt *next;

};

I want to be able to fill out the variables in this struct and then
send it to the server.

I do this using:
struct mySt pStruct;
pStruct.id = 1;
pStruct.val = 'a';
send ( sd, &pStruct, sizeof(struct pStruct), 0);

on the server though, even though I do receive this info. I have no
idea how to parse. I recieve a data in a variable of the same type
(struct mySt), but I don't know how to read it / parse it. Is it read
into char or can I do something like:
struct mySt **receivedPacket;
int id = receivedPacket[0];

Thanks
 
I

Ian Collins

Jack said:
Hi all,
I have a problem. I am writing a client server app in C. In my client
program, I have a simlple struct similiar to:
Please don't multi-post on Usenet, cross-post if you must, but don't
multi-post. You also asked this on comp.unix.programmer, which is
probably where it should be answered.
 
T

Tor Rustad

Jack said:
Hi all,
I have a problem. I am writing a client server app in C. In my client
program, I have a simlple struct similiar to:

struct mySt{
int id;
char val;
struct mySt *next;

};

Sending raw struct's over the wire is a really bad idea, even between
homogeneous systems.

Depending on the compiler used, different padding may be used.

So don't do that... for more advanced protocols ASN.1 is typically used,
but for simpler applications, you can just define endian order of
binary data, and pass each struct member along the wire.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top