Professional Web Applications Themes

Bits how to program them - UNIX Programming

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 ...

  1. #1

    Default 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


    Marcia Guest

  2. #2

    Default Re: Bits how to program them

    > I am writing a P2P client application. As such, I am creating packets 

    Probably easiest to work with an unsigned char array. The byte is the
    fundamental unit for data access. If you want to work on bits within a
    byte, you can do so with bitwise operators.
     

    Maybe this helps... I'm assuming everything in your above list is bytes.

    #define HEADER_SIZE 5
    #define SIZE0_OFFSET 0 /* size low byte */
    #define SIZE1_OFFSET 1 /* size byte byte */
    #define TYPE_OFFSET 2

    unsigned char header[HEADER_SIZE]; /* contiguous bytes in here */

    Now you can manipulate the individual fields like this:
    unsigned char message_type = header[TYPE_OFFSET];
    unsigned int length = header[SIZE0_OFFSET] + (header[SIZE1_OFFSET] << 8);

    The << 8 performs bit shift to form one large integer out of the two
    bytes.
     

    If you access the contents like this, then you can simply send your
    header buffer in one go, all bytes are in there.

    --
    Jem Berkes
    http://www.sysdesign.ca/
    Jem Guest

  3. #3

    Default Re: Bits how to program them



    Jem Berkes wrote:
     
    >
    >
    > Probably easiest to work with an unsigned char array. The byte is the
    > fundamental unit for data access. If you want to work on bits within a
    > byte, you can do so with bitwise operators.
    >
    >  [/ref]

    This is 6 bytes. Maybe this is part of the problem?
     
    This presupposes a particular endian-ness. May not be valid.

     
    >
    >
    > If you access the contents like this, then you can simply send your
    > header buffer in one go, all bytes are in there.
    > [/ref]

    The OP could also try Unions.


    --
    Ñ
    "It is impossible to make anything foolproof because fools are so
    ingenious" - A. Bloch

    Nick Guest

  4. #4

    Default Re: Bits how to program them

    >> #define HEADER_SIZE 5 
    > 8); 
    > This presupposes a particular endian-ness. May not be valid.[/ref]

    Since the OP is defining their own packet format (hence, knows the byte
    order) the code is quite portable. This is a better method than trying to
    memcpy bytes into an int space, or other goofy tricks like that.

    --
    Jem Berkes
    http://www.sysdesign.ca/
    Jem Guest

  5. #5

    Default Re: Bits how to program them

    > Hi, 
    Which is atleast 6 bytes.. 

    struct whatever {
    unsigned size:16;
    unsigned type:8;
    unsigned start:8;
    unsigned body:8;
    unsigned end:8;
    } __attribute__((__packed__));

    That's a gcc extension though.


    --
    Vennlig hilsen/Best Regards
    Nils Olav Selåsdal
    System Engineer
    w w w . u t e l s y s t e m s . c o m
    Nils Guest

  6. #6

    Default Re: Bits how to program them

    On Wed, 11 Feb 2004 07:34:29 GMT
    no (Nils O. Selåsdal) wrote:
     
    >
    > struct whatever {
    > unsigned size:16;
    > unsigned type:8;
    > unsigned start:8;
    > unsigned body:8;
    > unsigned end:8;
    > } __attribute__((__packed__));
    >
    > That's a gcc extension though.[/ref]
    Most compilers support packing, some by using #pragma, but it is usually
    implementation defined.

    Sending binary data over a socket connection is not problematic per se.,
    but portability could be an issue, especially between systems that are
    different endian models.
    --
    Jerry Feldman <gaf-nospam-at-blu.org>
    Boston Linux and Unix user group
    http://www.blu.org PGP key id:C5061EA9
    PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
    Jerry Guest

  7. #7

    Default Re: Bits how to program them

    "Marcia Hon" <com> writes:
     

    You don't need to send bits, you could send ASCII text as well.

    fprintf(socket,"(%u %u %u %u %u)\n",size,type,start,body,end);

    and a the other end:

    fscanf(socket,"(%u %u %u %u %u)\n",&size,&type,&start,&body,&end);

     


    Write it EXACTLY as you say it!

    unsigned char buffer[1024];
    put_bits(buffer,0,16,size);
    put_bits(buffer,24,8,type);
    put_bits(buffer,32,8,start);
    put_bits(buffer,40,8,body);
    put_bits(buffer,48,8,send);
    send_bits(buffer,54);


    --
    __Pascal_Bourguignon__ http://www.informatimago.com/
    There is no worse tyranny than to force a man to pay for what he doesn't
    want merely because you think it would be good for him.--Robert Heinlein
    http://www.theadvocates.org/
    Pascal Guest

  8. #8

    Default Re: Bits how to program them

    >> That's a gcc extension though. 

    But endian-ness shouldn't be an issue if you're sending character arrays
    and explicitly reconstructing integers based on your definition of high
    byte, low byte.

    --
    Jem Berkes
    http://www.sysdesign.ca/
    Jem Guest

Similar Threads

  1. 64 vs 32 bits OS
    By alexusbiz in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: September 13th, 09:25 PM
  2. running 64 bits apps on 32 bits hardware
    By Jacques-Henri in forum AIX
    Replies: 2
    Last Post: November 24th, 06:45 PM
  3. Replies: 2
    Last Post: November 1st, 03:06 AM
  4. What's the difference between 16 bits/channel and 8 bits /channel
    By dario in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 4
    Last Post: September 16th, 07:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139