Thread: Preparing packet to send

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Question Preparing packet to send

    Hi everybody! I need your help for a problem. I have to prepare a packet to send. The packet is a char* and it is divided into two parts:
    - the first one is a struct made in this way
    Code:
      struct data
     {
       char a;
       char b;
       short c;
       short dim;
       char e;
       char f;
       short data_points;
       char* value;
    }
    and its size is 16 bytes (sizeof(data)).

    - the second one is a vector (type short int) that contains values and its size depends on data.dim value.

    Now I have to concatenate the two strings and I do it in this way:

    1) I declare two char* variables
    char* buf1 = (char*)data;
    char* buf2 = (char*)value;

    2) I use char *strcat ( char * dest, const char * src ) function (<string.h>)
    char* buffer = strcat(buf1,buf2);

    Once that I have transmitted the package, I receive the correct number of bytes but I'm able to extract only the struct values from the buffer. So before sending the package I try to print out the buffer:
    Code:
     data* l;
     l = (data*)buffer;
     cout << "Data received: " << endl;
     cout << "A: " << l->a << endl;
     cout << "B: " << l->b << endl;
     cout << "C: " << l->c << endl;
     cout << "Dim: " << l->dim << endl;
     cout << "E: " << l->e << endl;
     cout << "F: " << l->f << endl;
     cout << "Data_Points: " << l->data_points << endl;
    and the values (struct data) printed are right.

    Now I try to print out the values (short int* values):
    Code:
    buffer+=sizeof(data);   //I move the pointer of sizeof(data) bytes (is it right???)
     short int* f;
     f = (short int*)buffer;
     cout << "Values: " << endl;
     for(int j=0; j<(data.data_points); j++)
     {
        cout << " Ordinate " << f[j];
      }
    and the values printed are wrong.

    Where is the mistake?
    Thanks.
    -David-
    p.s.: Sorry for my poor english...

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot use strcat to concatenate the two variables into one char* variable because strcat determines the size of the data to copy by finding 0-chars in both strings. It will propably not work as expected. Use memcpy instead.
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    With memcpy it works fine.
    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    memcpy doesn't allocate memory either. Allocate a new string with malloc, and then copy them both into it.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to send a FIN with my last packet???
    By klipseracer in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2008, 12:40 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Raw Packet (sorry tripple weird Post)
    By Coder87C in forum Networking/Device Communication
    Replies: 6
    Last Post: 03-04-2006, 11:34 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM