467,877 Members | 1,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,877 developers. It's quick & easy.

Sending a struct over TCP

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

Mar 30 '07 #1
  • viewed: 4045
Share:
2 Replies
Jack wrote:
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.

--
Ian Collins.
Mar 30 '07 #2
Jack wrote:
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.

--
Tor
Mar 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Leo | last post: by
2 posts views Thread by Fernando Barsoba | last post: by
9 posts views Thread by thorley | last post: by
2 posts views Thread by Sean | last post: by
8 posts views Thread by Thomas Dybdahl Ahle | last post: by
6 posts views Thread by Jack | last post: by
4 posts views Thread by =?Utf-8?B?RWl0YW4=?= | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.