title: Protocol Buffer Corrupting Data
body: <p>This is my first question on here and first project on my own. I have a client (C++ on Windows 10) using the SimConnect SDK to pull data from the Prepar3D Flight Simulator and a server (C on Ubuntu) that is receiving this data. I am using sockets and Protocol Buffers. The client receives the data from the sim, maps it to a protocol buffer and sends it. The server receives it from the socket, decodes the protocol buffer and then processes it. The issue is that the last field (dWindDirection) is always corrupted on the receiving end. I'm not entirely sure how/why it's being corrupted but it's consistent as in the same input to the socket always leads to the same corrupted output from the socket. I followed <a href="https://stackoverflow.com/questions/9496101/protocol-buffer-over-socket-in-c">Protocol Buffer over socket in C++</a> for the use of protocol buffers over a socket. I have included my code below as well as the output.</p>
  <p>Source code can also be found here. <a href="https://github.com/nbreen/Simconnect-Data-Client" rel="nofollow noreferrer">https://github.com/nbreen/Simconnect-Data-Client</a></p>
  <p>Client function that sends the protobuff</p>
  <pre><code>DWORD WINAPI createProto(LPVOID lParam) {
      ObjectData* toConvert = (ObjectData*)lParam;
      fopen_s(&amp;dataOut,&quot;dataOut.txt&quot;, &quot;a+&quot;);
      fprintf(dataOut, &quot;Before serialiing \n&quot;);
      logData(toConvert);
      simConnect::simData convertedData;
      std::string toStr(toConvert-&gt;szTitle);
  
      convertedData.set_sztitle(toStr);
      convertedData.set_dabsolutetime(toConvert-&gt;dAbsoluteTime);
      convertedData.set_dtime(toConvert-&gt;dTime);
      convertedData.set_usimonground(toConvert-&gt;uSimOnGround);
      convertedData.set_daltitude(toConvert-&gt;dAltitude);
      convertedData.set_dheading(toConvert-&gt;dHeading);
      convertedData.set_dspeed(toConvert-&gt;dSpeed);
      convertedData.set_dverticalspeed(toConvert-&gt;dVerticalSpeed);
      convertedData.set_dgpseta(toConvert-&gt;dGpsEta);
      convertedData.set_dlatitude(toConvert-&gt;dLatitude);
      convertedData.set_dlongitude(toConvert-&gt;dLongitude);
      convertedData.set_dsimtime(toConvert-&gt;dSimTime);
      convertedData.set_dtemperature(toConvert-&gt;dTemperature);
      convertedData.set_dairpressure(toConvert-&gt;dPressure);
      convertedData.set_dwindvelocity(toConvert-&gt;dWindVelocity);
      convertedData.set_dwinddirection(toConvert-&gt;dWindDirection);
  
      printf(&quot;Size after serializing is %ld\n&quot;, convertedData.ByteSizeLong());
      long pktSize = convertedData.ByteSizeLong();
      fprintf(dataOut, &quot;After serializing before socket\n%s\n\n&quot;, convertedData.DebugString().c_str());
      char* pkt = new char[pktSize];
      google::protobuf::io::ArrayOutputStream aos(pkt, pktSize);
      google::protobuf::io::CodedOutputStream* coded_pkt = new google::protobuf::io::CodedOutputStream(&amp;aos);
      coded_pkt-&gt;WriteVarint64(convertedData.ByteSizeLong());
      convertedData.SerializeToCodedStream(coded_pkt);
      int iResult = send(clientSocket, pkt, pktSize, 0);
      printf(&quot;Sent bytes %d\n&quot;, iResult);
      fclose(dataOut);
  
      return 0;
  }
  </code></pre>
  <p>Server function that receives and processes the protobuff</p>
  <pre><code>while(1) {
          result = recv(client, sizeBuff, 4, MSG_PEEK);
          printf(&quot;Receive is %d\n&quot;, result);
          if (result == -1) {
              printf(&quot;Error receiving data with byteSize\n&quot;);
              break;
          }else if (result == 0) {
              break;
          }
  
          if (result &gt; 0) {
              printf(&quot;First read byte count is %d\n&quot;, result);
              readMessage(client, readHeader(sizeBuff));
          }
      }
  
  google::protobuf::uint64 readHeader(char *buf) {
    google::protobuf::uint64 size;
    google::protobuf::io::ArrayInputStream ais(buf,4);
    google::protobuf::io::CodedInputStream coded_input(&amp;ais);
    coded_input.ReadVarint64(&amp;size);//Decode the HDR and get the size
    std::cout&lt;&lt;&quot;size of payload is &quot;&lt;&lt;size&lt;&lt;std::endl;
    return size;
  }
  
  void readMessage(int csock, google::protobuf::uint64 siz) {
    int bytecount;
    simConnect::simData payload;
    char buffer [siz+4];//size of the payload and hdr
    //Read the entire buffer including the hdr
    if((bytecount = recv(csock, (void *)buffer, 4+siz, 0))== -1){
                  fprintf(stderr, &quot;Error receiving data %d\n&quot;, errno);
          }
    std::cout&lt;&lt;&quot;Second read byte count is &quot;&lt;&lt;bytecount&lt;&lt;std::endl;
    //Assign ArrayInputStream with enough memory 
    google::protobuf::io::ArrayInputStream ais(buffer,siz+4);
    google::protobuf::io::CodedInputStream coded_input(&amp;ais);
    //Read an unsigned integer with Varint encoding, truncating to 32 bits.
    coded_input.ReadVarint64(&amp;siz);
    //After the message's length is read, PushLimit() is used to prevent the CodedInputStream 
    //from reading beyond that length.Limits are used when parsing length-delimited 
    //embedded messages
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(siz);
    //De-Serialize
    payload.ParseFromCodedStream(&amp;coded_input);
    //Once the embedded message has been parsed, PopLimit() is called to undo the limit
    coded_input.PopLimit(msgLimit);
    //Print the message
    //std::cout&lt;&lt;&quot;Message is &quot;&lt;&lt;payload.DebugString();
    FILE *outFile = fopen(&quot;out.txt&quot;, &quot;a+&quot;);
    /*std::string strPkt(buffer);
    payload.ParseFromString(strPkt);*/
    fprintf(outFile, &quot;From socket\n%s\n\n&quot;, payload.DebugString().c_str());
    fclose(outFile);
    cudaProcess(payload);
  }
  </code></pre>
  <p>Client Output</p>
  <pre><code>Before serialiing 
  Title: F-22 Raptor - 525th Fighter Squadron
  Absolute Time: 63631767652.278290 seconds
  Zulu Time: 68452.281250 Seconds
  Sim On Ground: 0 
  Altitude: 11842.285630 Feet
  Heading: 3.627703 Radians
  Speed: 426.008209 Knots
  Vertical Speed: 596.607849 Feet Per Second
  GPS ETA: 0.000000 Seconds
  Latitude: 30.454685 Degrees
  Longitude: -86.525197 Degrees
  Sim Time: 2996.388142 Seconds
  Temperature: -8.145923 Celsius
  Air Pressure: 648.718994 Millibars
  Wind Velocity: 36.988354 Feet Per Second
  Wind Direction: 270.000000 Degrees
  
  
  After serializing before socket
  szTitle: &quot;F-22 Raptor - 525th Fighter Squadron&quot;
  dAbsoluteTime: 63631767652.27829
  dTime: 68452.28125
  usimOnGround: 0
  dAltitude: 11842.285629708613
  dHeading: 3.6277025313026936
  dSpeed: 426.00820922851562
  dVerticalSpeed: 596.60784912109375
  dGpsEta: 0
  dLatitude: 30.454685493870045
  dLongitude: -86.525197127202063
  dSimTime: 2996.388142343636
  dTemperature: -8.1459226608276367
  dAirPressure: 648.718994140625
  dWindVelocity: 36.988353729248047
  dWindDirection: 270
  </code></pre>
  <p>Server Output</p>
  <pre><code>From socket
  szTitle: &quot;F-22 Raptor - 525th Fighter Squadron&quot;
  dAbsoluteTime: 63631767652.27829
  dTime: 68452.28125
  usimOnGround: 0
  dAltitude: 11842.285629708613
  dHeading: 3.6277025313026936
  dSpeed: 426.00820922851562
  dVerticalSpeed: 596.60784912109375
  dGpsEta: 0
  dLatitude: 30.454685493870045
  dLongitude: -86.525197127202063
  dSimTime: 2996.388142343636
  dTemperature: -8.1459226608276367
  dAirPressure: 648.718994140625
  dWindVelocity: 36.988353729248047
  dWindDirection: 1.2168372663711258e-309 &lt;-- This is always the corrupt value when Wind direction is 270
  </code></pre>