title: Strange '??????' received when deserializing from JSON via UDP
body: <p>I have a client application which sends commands to another application, the server, via UDP.
   Both client and server application are running on the same machine.</p>
  
  <p>First off, for reference if needed, here is a link to my previous post a couple of hours ago: <a href="https://stackoverflow.com/questions/34465523/c-deserialize-object-sent-from-c-sharp-application-via-udp">c++ deserialize object sent from c# application via UDP</a></p>
  
  <p>My client application is in C#. It simply gets the key pressed by the user, a time stamp of when the key was pressed and bundles the two together in my class "Package". Next, "Package" is serialized into JSON:</p>
  
  <pre><code>Package package = new Package();
  package.RoundTripDelay = DateTime.Now;
  package.PressedKey = MOVEMENT.Forward;
  var json = new JavaScriptSerializer().Serialize(package);
  </code></pre>
  
  <p>The client then sends this json via UDP to my remote application, the server application, which happens to be an <a href="https://www.unrealengine.com/what-is-unreal-engine-4" rel="nofollow noreferrer">Unreal Engine 4.10</a> app:</p>
  
  <pre><code>byte[] bytes = Encoding.ASCII.GetBytes(json);
  client.Send(bytes, bytes.Length, ip);
  </code></pre>
  
  <p>On the server-side of things, the json is received and then parsed using the <a href="http://rapidjson.org/index.html" rel="nofollow noreferrer">rapidjson library</a>.</p>
  
  <p>I won't go into the details of how the server code looks, but just so that you get the full picture, here is how I deal with the incoming UDP packet:</p>
  
  <p><strong>UE4 Server application</strong></p>
  
  <pre><code>void AUDPActor::ProcessUDPPacket(TArray&lt;uint8&gt; packetData) {
  
      FString message = StringFromBinaryArray(packetData);
      string receivedJson(TCHAR_TO_UTF8(*message));
  
      ProcessJSONUDPPacket(receivedJson);
  
      //Display the received message on the GUI.
      GEngine-&gt;AddOnScreenDebugMessage(-1, 5.f, FColor::Red, message);
  
  }
  
  //Handy function from RAMA.
  FString AUDPActor::StringFromBinaryArray(const TArray&lt;uint8&gt;&amp; BinaryArray)
  {
      //Create a string from a byte array!
      const std::string cstr(reinterpret_cast&lt;const char*&gt;(BinaryArray.GetData()), 
                                                           BinaryArray.Num());
  
      //FString can take in the c_str() of a std::string
      return FString(cstr.c_str());
  }
  
  void AUDPActor::ProcessJSONUDPPacket(string json) {
  
      Document document;
  
      document.Parse(json.c_str());
  
      if (document.HasMember("PressedKey")) {
          int pressedKey = document["PressedKey"].GetInt();
          ApplyReceivedInput(pressedKey);
      }
  
  }
  
  void AUDPActor::ApplyReceivedInput(int number) {
  
      switch (number)
      {
      case 100:
          break;
      case 0: //forward
          myCharacter-&gt;AddMovementInput(FVector(10, 0, 0), 5.0);
          break;
      case 1: //backward
          myCharacter-&gt;AddMovementInput(FVector(-10, 0, 0), 5.0);
          break;
      case 2: //left
          myCharacter-&gt;AddMovementInput(FVector(0, -10, 10), 5.0);
          break;
      case 3: //right
          myCharacter-&gt;AddMovementInput(FVector(0, 10, 0), 5.0);
          break;
      }
  
  }
  </code></pre>
  
  <p>Now, ignore the fact that there are some missing error checks here. The problem is that when I tap "w", which corresponds to "forward" in the <code>void AUDPActor::ApplyReceivedInput(int number)</code> function, the character moves forward. However, if I hold the key down ("w", that is), the character does not move. </p>
  
  <p>When I inspect the JSON received: <code>FString message = StringFromBinaryArray(packetData);</code> by displaying it on the GUI: <code>GEngine-&gt;AddOnScreenDebugMessage(-1, 5.f, FColor::Red, message);</code>, I notice a bunch of "??????????"'s tacked onto the end of my JSON message... </p>
  
  <p>Take a look at this image:</p>
  
  <p><a href="https://i.stack.imgur.com/PTrks.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PTrks.jpg" alt="enter image description here"></a></p>
  
  <p>On the right (the client) you can see the message (JSON) being sent. "PressedKey":0 represents a press of "w" and simply means... walk forward.</p>
  
  <p>Now, on the left, you have the server application (UE4), which is showing the message received. All is well if I just tap the keys, but holding them down results in the strange line of ??????? that can be seen in the image above.</p>
  
  <p>What could possibly be the reason? </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've just had a thought. Amazing, I know, but is it possible that the buffer is filled with garbage? In other words, perhaps my buffer is of size 100 and my data is really only 30 bytes. Then, would the buffer not contain some garbage from the UDP packet? Clearly, I'm not familiar with network programming, but I am wondering if I need to "flush" my socket or something like that.</p>
  
  <p>In any case, I've yet to make any progress with this issue.</p>