Bits How to program them

M

Marcia Hon

Hi,

I am writing a P2P client application. As such, I am creating packets that
are to be sent between the peers. I would like to know how in C these bits
of the packets may be programmed. These bits need to be continguous.

For example: I create a packet of size 5. 2 bytes for the size, 1 for the
message type, 1 for the start flag, 1 for the body, and 1 for the end flag.

How to I program this?

I have tried the following:

unsigned size:16;
unsigned type:8;
unsigned start:8;
unsigned body:8;
unsigned end:8;

How to I keep them contiguous? I would like to eventually send them over the
socket connection. And therefore would like to store them in an unsigned *
buffer.

Please, please help.

Thank you,
Marcia
 
T

Tom St Denis

Marcia Hon said:
How to I keep them contiguous? I would like to eventually send them over the
socket connection. And therefore would like to store them in an unsigned *
buffer.

The only semi-portable solution is to pack the bits in an array of chars
yourself and send that to your socket functions [e.g. portable to any BSD
derived sockets code].

Tom
 
M

Marcia Hon

If I were to want to state that the size is 300 which is over 1 byte. How
would I give that value to variable?

I have a variable unsigned * buffer, that stores the first 2 bytes as the
size. So, do I state *buffer = 300? Or is there some other way to implement
the first 2 bytes as representing the value of 300?

Thanks so much for your help,
Marcia Hon
 
T

Tom St Denis

Marcia Hon said:
If I were to want to state that the size is 300 which is over 1 byte. How
would I give that value to variable?

I have a variable unsigned * buffer, that stores the first 2 bytes as the
size. So, do I state *buffer = 300? Or is there some other way to implement
the first 2 bytes as representing the value of 300?

I'd say an important first step is to take some basic programming courses
before trying to write a P2P app ;-)

Tom
 
M

Mac

If I were to want to state that the size is 300 which is over 1 byte. How
would I give that value to variable?

I have a variable unsigned * buffer, that stores the first 2 bytes as the
size. So, do I state *buffer = 300? Or is there some other way to implement
the first 2 bytes as representing the value of 300?

Thanks so much for your help,
Marcia Hon


Hmmm. You seem to be a bit lost.

If you allocate space, somehow, and make buffer point at that space, then
you can do what you say:

*buffer = 300;

But depending on what the type is of *buffer, this may not have the effect
that you want. In particular, if buffer is a pointer to unsigned char,
then on typical platforms, where CHAR_BIT is 8, the above code will be
eqivalent to:

*buffer = 300 - 256;

I think you need to read a book or two on C, and maybe write some simpler
programs before you mess with network programming.

Mac
--
 
Y

Yakov Lerner

Marcia Hon said:
Hi,

I am writing a P2P client application. As such, I am creating packets that
are to be sent between the peers. I would like to know how in C these bits
of the packets may be programmed. These bits need to be continguous.

For example: I create a packet of size 5. 2 bytes for the size, 1 for the
message type, 1 for the start flag, 1 for the body, and 1 for the end flag.

How to I program this?

I have tried the following:

unsigned size:16;
unsigned type:8;
unsigned start:8;
unsigned body:8;
unsigned end:8;

How to I keep them contiguous? I would like to eventually send them over the
socket connection. And therefore would like to store them in an unsigned *
buffer.


You keep them contiguous by putting them either into array, or into a struct.
Here is example with array:

void pack(unsigned char packet[6], ushort size, uchar type, uchar start,
uchar body, uchar end)
{
packet[0] = size & 0xFF;
packet[1] = (size >> 8) & 0xFF;
packet[2] = type;
packet[3] = start;
packet[4] = body;
packet[5] = end;
}

But note that 2+1+1+1+1 is 6 not 5.

Y.L.
 

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
474,139
Messages
2,570,805
Members
47,355
Latest member
MavoraTech

Latest Threads

Top