title: Sending strings over the net from C++ client to Java server
body: <p>I'm writing a client/server application in which, client is written in C++ and the server is Java. The communication between them is made by the UDP protocol.
  They need to exchange string messages over the net.
  Now, the communication works a lot, the client sends message and server receives it, but I noticed that the received string, on the Java side, is like trunked. I mean, if I try to display it onto the console, with the function: </p>
  
  <pre><code>System.out.println("This is the message received " 
                             + message + " by the client just now");
  </code></pre>
  
  <p>the result I obtain is:</p>
  
  <pre><code>This is the message received *message*
  </code></pre>
  
  <p>with the string "by the client just now" trunked out.</p>
  
  <p>I think it's due to some incompatibility between Java and C++, but I can't found out the solution.</p>
  
  <p>edit: 
  here's the code of the receiver: </p>
  
  <pre><code>byte[] bytes = _packet.getData(); // Datagram Packet
  hostName = getStringFromBytes(bytes, 0, 15);
  
  (...)
  
  private String getStringFromBytes (byte[] bytes, int lowerBound, int length) 
  {
      byte[] bufferBytes  = new byte[length];
      System.arraycopy(bytes, lowerBound, bufferBytes, 0, length);
  
      return new String(bufferBytes).trim();
  }
  </code></pre>
  
  <p>and the sender:</p>
  
  <pre><code> if(sendto(_socket, buffer, BUFFER_LEN, 0, (struct sockaddr*) &amp;_serverAddress, addressLenght) == -1)
      cout &lt;&lt; "Trasmission failed!\n" &lt;&lt; endl;
  </code></pre>
  
  <p>where <em>buffer</em> is an array of char.</p>