Help - trying to serialize a struct

E

erbm

hello guys. I'm trying to converting a struct to a buffer of bytes, to
send to a server using sockets... I tryed a lot of solutions, but
nothing worked. I can convert the struct to a char* (string
representation) , but that isn't the point... I must send bytes to the
server and he must be able to deserialize the information. The
functions:

//return the number of alocated bytes
int message_to_buffer(struct message_t *msg, char **buffer);

//Deserialize the struct
struct message_t *buffer_to_message(int buflen, char *buffer);

the seralization should be something like this: (an array of bytes)

OPCODE KEYSIZE KEY DATASIZE DATA

[2byte] [4byte] [KEYSIZE bytes] [4 byte] [DATASIZE bytes]


And the structs:

struct message_t {

short opcode;

struct key_t key;

struct data_t data;

};

struct key_t{

int keysize;
char *key;
};



struct data_t{

int datasize;
char *data;
};

Does anyone have an ideia, how I can do that?
thanks
 
C

CBFalconer

erbm said:
i guess my problem is to get the bytes of a data. For example,
if I have:

int i = 658;
char* hello = "hello";

and I wanted to convert both to a byte array to send to the
server, and then recover de int and char*, how could I do it?

You are not understandable. Read the following links:

Some useful links on quoting:
<http://www.xs4all.nl/~wijnands/nnq/nquote.html>
<http://www.complang.tuwien.ac.at/anton/mail-news-errors.html>
<http://www.netmeister.org/news/learn2quote2.html>
<http://www.star-one.org.uk/computer/format.htm>
 
F

Flash Gordon

Paul said:
erbm said:
Paul's idea about a packed union struct.I'm new programing with C. Is
there a simple solution?

typedef struct
{
int value1;
int value2;
int value3;
}
MyStructType;

union
{
MyStructType TheStruct;
char Bytes[sizeof(MyStructType)];
}
MyUnion;

so you can do:

MyUnion.TheStruct.Value1 = 5;
MyUnion.TheStruct.Value2 = 6;
MyUnion.TheStruct.Value3 = 7;

for (EachBye = 0; EachByte < sizeof(MyStructType); EachByte++)
{
SendAChar(MyUnion.Bytes[EachByte]);
}

This is not sufficient since the OP has pointers in his struct, so
proper serialisation/deserialisation functions are needed. Also your
method will hit problems if the server has a different endianness or
different type sizes.
 
C

CBFalconer

Richard said:
CBFalconer said:

I haven't seen the article to which you're replying, but even from
the tiny amount of information you have quoted (which might be all
of it) it's pretty obvious what the OP wants. Claiming that he (or
she) is not understandable is laughable. If *you* don't understand
the OP, that's hardly newsworthy, and other people are not saddled
with the same comprehension limitations as you.

<snip>

Why do you insist on jumping in with snide comments after snipping
the informative portion of a message? My original gave erbm a herd
of referances that expanded on my one line comment, and explained
what he was doing wrong. You obviously prefer that he remain
ignorant and continue to make non-understandable postings in
future. Can you possibly think that your comments are helpful to
anyone? I thought you were more intelligent than that, but I may
have to revise my placement.
 
F

Flash Gordon

Paul said:
Yes, I did mention both of these constraints further up the thread.

OK, I didn't see (or I forgot) that post.
I've found they're not too difficult to work around in practice.

I agree they are easy to deal with.
 
C

CBFalconer

Richard said:
CBFalconer said:
.... snip ...


There are some people whose opinions of me I consider to be
significant and worth my attention. You used to be one of them,
but have not been so for some years now.

You might ponder the idea that the difference lies within you,
rather than me.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,010
Latest member
MerrillEic

Latest Threads

Top