#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    32
    Rep Power
    13

    implementing high-level checksum


    what i'm trying to do is to implement a (very) high-level checksum for my TCP client app written in C...

    here's the data format for the packets i'm sending out from the client:
    packet1-1033452719-878475\

    what i want to do is to do a sum of the ASCII values of each indivual character (using ord())...

    so it would go something like this:
    sum = ord('p') + ord('a') + ord('c') + ... + ord(5)

    then after getting the checksum, i want to add it at the end of the packet too, like so:
    packet1-1033452719-878475-checksum\

    how would i go about doing this? most importantly is how i can iterate over each character of the character array "packet1-1033452719-878475\" and perform the summation

    thanks!
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    How about some code like this:
    Code:
    unsigned char compute_checksum(unsigned char *buf) {
       unsigned char *ch, result = 0;
    
       /* buf is assumed to be null terminated */
       for (ch = buf; *ch; ch++)
          result += (*ch);
    
       return result;
    }
    ....
    unsigned char checksum;
    ....
    ....
    checksum = compute_checksum(buffer);
    /* add checksum to end of buffer */
    buffer[len] = checksum;
    buffer[len+1] = '\0'; /* buffer assumed to be at least len+1 chars */
    Please note that I typed this code off the top of my head, so it may have a few syntax errors. This isn't even a very good checksum algorithm, it just adds the ascii value of the chars up. You'd probably want to do a standard 16 or 32 bit CRC algorithm to ensure more reliability. You can probably google for those :) Hope this helps!
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    32
    Rep Power
    13
    that was fast... thanks man! :)

    it looks good and now i'm gonna see if it's worthwhile to implement a checksum... i just realised that if i do the checksum computation, it may reduce my packet sending rate which i'm trying to get to be as accurate as possible

    thanks again!

IMN logo majestic logo threadwatch logo seochat tools logo