title: ProtocolBuffer can't define data although it is received
body: <p>I'm trying to use Google's ProtocolBuffer to send/receive data in a server/client architecture. I am able to connect the two with winsock and I am able to send and receive data from ProtocolBuffer with it but there seems to be a problem with deserializing the data on the server (I haven't done the opposite so I can't tell if that works yet)</p>
  
  <p>Here's the code I use to create a sample packet of data to send to the server:</p>
  
  <pre><code>MessageID *message = new MessageID();
  message-&gt;set_type(MessageID::Type::MessageID_Type_PLAYERDATA);
  
  PlayerData::Vector2 *position = new PlayerData::Vector2();
  position-&gt;set_x(10.0f);
  position-&gt;set_y(25.0f);
  
  PlayerData *data = new PlayerData();
  data-&gt;set_health((UINT32)50);
  data-&gt;set_allocated_position(position);
  
  auto inventory = data-&gt;add_items();
  inventory-&gt;set_itemid((INT32)1);
  inventory-&gt;set_count((INT32)5);
  
  message-&gt;set_allocated_playerdata(data);
  
  m_pNetworkObject-&gt;Send(message);
  </code></pre>
  
  <p>This is the code that actually sends the data over a TCP connection:</p>
  
  <pre><code>// Serialize to string.
  std::string sendData;
  data-&gt;SerializeToString(&amp;sendData);
  
  // Convert data to const char*
  const char* dataToSend = sendData.c_str();
  
  int iResult;
  iResult = send( ConnectSocket, dataToSend, data-&gt;ByteSize(), 0 );
  if (iResult == SOCKET_ERROR) 
  {
      printf("send failed with error: %d\n", WSAGetLastError());
      closesocket(ConnectSocket);
      WSACleanup();
      exit(1);
  }
  </code></pre>
  
  <p>So, this was code that is run on the client. Now, when I receive this on the server I get the following errors from ProtocolBuffer:</p>
  
  <pre><code>Can't parse message of type "MessageID" because it is missing required fields: type
  Can't parse message of type "PlayerData" because it is missing required fields: Position, Health
  </code></pre>
  
  <p>The code on the server that decompiles the winsock data is like this:</p>
  
  <pre><code>// decompile to a proto file and check ID
  MessageID* receivedData = new MessageID();
  receivedData-&gt;ParseFromArray(&amp;recv, strlen(recv));
  
  // check and redeserialize
  switch (receivedData-&gt;type())
  {
  case MessageID::Type::MessageID_Type_PLAYERDATA:
      {
      PlayerData* playerData = new PlayerData();
      playerData-&gt;ParseFromArray(&amp;recv, strlen(recv));
      UsePlayerData(sock, playerData);
      break;
      }
  case MessageID::Type::MessageID_Type_WORLDDATA:
      {
      WorldData* worldData = new WorldData();
      worldData-&gt;ParseFromArray(&amp;recv, strlen(recv));
      UseWorldData(sock, worldData);
      break;
      }
  }
  </code></pre>
  
  <p>The weird thing is that, although the error says that the data doesn't contain the Type value it does set it to 1 and the switch statement works. After that when I try to take a look at the Position and Health data everything is 0.</p>
  
  <p>I have no idea what I'm doing wrong. I read most of the ProtocolBuffer tutorials and help but it seems to no avail.</p>
  
  <p>EDIT: 
  I tried serializing and then deserializing the data in the same application (without the networking involved) and with the following code it worked to do just that:</p>
  
  <pre><code>std::string sendData;
  message-&gt;SerializeToString(&amp;sendData);
  
  MessageID* receivedData = new MessageID();
  receivedData-&gt;ParseFromString(sendData);
  </code></pre>
  
  <p>If I check the data in receivedData it is exactly the same (so, the right values) as in the original object "message"</p>