473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Bits How to program them

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
Nov 14 '05 #1
6 1609

"Marcia Hon" <ho**@rogers.com> wrote in message
news:Xy****************@news01.bloor.is.net.cable. rogers.com...
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
Nov 14 '05 #2
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
Nov 14 '05 #3

"Marcia Hon" <ho**@rogers.com> wrote in message
news:3I******************@twister01.bloor.is.net.c able.rogers.com...
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
Nov 14 '05 #4
Mac
On Wed, 11 Feb 2004 01:38:07 +0000, Marcia Hon wrote:
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
--

Nov 14 '05 #5
Marcia Hon wrote:

1 - create buffer as a character array
2 - use memcpy() or its equivalent to store data in the array
3 (a) - create tcp socket
3 (b) - use sendt() or write(0 to send data on the socket.

relating code can be found on
"http://www.ecst.csuchico.edu/~beej/guide/net/html/"
- phax

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


Nov 14 '05 #6
"Marcia Hon" <ho**@rogers.com> wrote in message news:<Xy****************@news01.bloor.is.net.cable .rogers.com>...
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.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Abby | last post by:
Hi all, I need advice on how to concatenate bits value. I have 2 bytes of Hex value: Byte1 = 0x33 --> 00110011 Byte2 = 0x90 --> 10010000 I want 10 bits value which compose of 2 bits(7th...
11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
5
by: Oyvind Eriksen | last post by:
Hello. I need to read bits from bytes in a file. I have code that works but it's very slow. Can anybody help me? The code I have is: private bool GetBit(byte b, int pos) { return ((b &...
16
by: chandanlinster | last post by:
As far as I know floating point variables, that are declared as float follow IEEE format representation (which is 32-bit in size). But chapter1-page no 9 of the book "The C programming language"...
23
by: Umesh | last post by:
This is a basic thing. Say A=0100 0001 in ASCII which deals with 256 characters(you know better than me!) But we deal with only four characters and 2 bits are enough to encode them. I want to...
77
by: borophyll | last post by:
As I read it, C99 states that a byte is an: "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment" (3.6) and that a byte...
6
by: John Messenger | last post by:
I notice that the C standard allows padding bits in both unsigned and signed integer types. Does anyone know of any real-world examples of compilers that use padding bits? -- John
11
by: JoeC | last post by:
I am working on a graphics program but my question has nothing to do with graphics but trying to get an algorithm to work. I set graphics from a 16x16 grid to bits of a graphic with: bitData =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.