title: Serialization issues while sending struct over socket
body: <p>I am developing a Client/Server based on UDP I want to send different messages to the client from the server.  There are different C structures defined for each message.</p>
  
  <p>I would like to understand what is wrong in the way I am serializing the data.</p>
  
  <pre><code>struct Task
  {
      int mType;
      int tType;
      int cCnt;
      int* cId;
      char data[128];
  };
  </code></pre>
  
  <p><strong>Serialization/Deserialization functions</strong>  </p>
  
  <pre><code>unsigned char * serialize_int(unsigned char *buffer, int value)
  {
      buffer[0] = value &gt;&gt; 24;
      buffer[1] = value &gt;&gt; 16;
      buffer[2] = value &gt;&gt; 8;
      buffer[3] = value;
      return buffer + 4;
  }
  
  unsigned char * serialize_char(unsigned char *buffer, char value)
  {
      buffer[0] = value;
      return buffer + 1;
  }
  
  int deserialize_int(unsigned char *buffer)
  {
      int value = 0;
  
      value |= buffer[0] &lt;&lt; 24;
      value |= buffer[1] &lt;&lt; 16;
      value |= buffer[2] &lt;&lt; 8;
      value |= buffer[3];
      return value;
  
  }
  
  char deserialize_char(unsigned char *buffer)
  {
      return buffer[0];
  }
  </code></pre>
  
  <p><strong>Sender side code to serialize the structure</strong>  </p>
  
  <pre><code>unsigned char* serializeTask(unsigned char* msg, const Task* t)
  {
      msg = serialize_int(msg,t-&gt;mType);
      msg = serialize_int(msg,t-&gt;tkType);
      msg = serialize_int(msg,t-&gt;cCnt);
      for(int i=0; i&lt;t-&gt;cCnt; i++)
              msg = serialize_int(msg,t-&gt;cId[i*4]);
  
  for(int i=0; i&lt;strlen(data); i++)
      msg = serialize_char(msg,t-&gt;data[i]);
  
      return msg;
  }
  </code></pre>
  
  <p><strong>Receiver side code to de-serialize data</strong>    </p>
  
  <pre><code>printf("Msg type:%d\n", deserialize_int(message) );
  printf("Task Type:%d\n", deserialize_int(message+4) );
  printf("Task Count:%d\n", deserialize_int(message+8));
  
  Output 
  Msg type:50364598         //Expected value is 3
  Task Type:-2013036362     //Expected value is 1
  Task Count:1745191094     //Expected value is 3  
  </code></pre>
  
  <p><strong>Question 1:</strong><br>
  Why is the de-serialized value not same as expected?  </p>
  
  <p><strong>Question 2:</strong><br>
  How is serialization/de-serialization method different from memcpy?  </p>
  
  <pre><code>Task t;
  memcpy(&amp;t, msg, sizeof(t));  //msg is unsigned char* holding the struct data  
  </code></pre>
  
  <p><strong>EDIT</strong>  </p>
  
  <p>Code which invokes <code>serializeTask</code>  </p>
  
  <pre><code>void addToDatabase(unsigned char* message, int msgSize, Task* task)
  {
      message = new unsigned char[2*msgSize+1];
      unsigned char* msg = message;  //To preserve start address of message
      message = serializeTask(message, task); //Now message points to end of the array
  
  //Insert serialized data to DB
  //msg is inserted to DB 
  }
  </code></pre>
  
  <p>Serialized data stored in DB  </p>
  
  <pre><code>Message:
  00 
  03 70 B6 88 03 70 B6 68 05 70 B6 68 05 70 B6 00 
  00 00 00 00 00 00 00 A8 05 70 B6 AC 05 70 B6 B4 
  05 70 B6 C9 05 70 B6 DE 05 70 B6 E6 05 70 B6 EE 
  05 70 B6 FB 05 70 B6 64 65 66 00 63 6F 68 6F 72 
  74 73 00 70 65 6E 64 69 6E 67 5F 61 73 73 69 67 
  6E 5F 74 61 73 6B 73 00 70 65 6E 64 69 6E 67 5F 
  61 73 73 69 67 6E 5F 74 61 73 6B 73 00 6D 65 73 
  73 61 67 65 00 6D 65 73 73 61 67 65 00 3F 00 FF 
  FF 00 00 FC 90 00 00 00 00 00 00 00 C9 2D B7 00 
  00 00 00 10 06 70 B6 00 00 00 00 00 00 00 00 30 
  06 70 B6 34 06 70 B6 3C 06 70 B6
  </code></pre>