title: sending a png from c++ server to java client
body: <p>I'm trying to send a png image from a c++ server to a Java (android studio) client (the image is saved on the server). I read that I need to encode the image in base64, send it, decode it, convert it to a byte array and then to bitmap. (I should probably mention at this point that I'm not that familiar with android studio). To be honest, I don't even know if I need to do all that, that's just what I found on the internet...</p>
  
  <p>Anyway, so I started doing that and here's what I've got:
  (This is the c++ server part, Message is a class I made and it handles part of the socketing-magic. Also, I should mention most of these functions were not made by me. And don't worry about the way the protocol looks like in sendImage, I just added it so that you can see how the image is sent. I'm using <a href="https://github.com/tkislan/base64" rel="nofollow noreferrer">https://github.com/tkislan/base64</a> for the base64 encoding)</p>
  
  <pre><code>void MessageHandler::handleSendPicture(Message &amp; msg)
  {
      SOCKET&amp; socket = msg.getSocket();
      char* image;
      std::ifstream f;     
      f.open("C:\\images\\A.png", ios::binary);
  
  
      if (f.is_open())
      {
          f.seekg(0, std::ios::end);
          unsigned int length = f.tellg();
          f.seekg(0, std::ios::beg);
  
          image = new char[length];
          f.read(image, length);
  
          f.close();
      }
  
      else
      {
          image = (char*)"Empty";
      }
  
      Message to_client = Message(msg.getSocket(), SEND_PIC, image);
      to_client.sendImage();
  }
  
  
  void Message::sendImage(unsigned int size, char* image)
  {
      string prefix = "04" + getPaddedNumber(size, 5);
      string message = prefix;
  
      for (unsigned int i = 0; i &lt; size; i++)
      {
          message += image[i];
      }
  
      const char* data = message.c_str();
      cout &lt;&lt; "message size: " &lt;&lt; message.size() &lt;&lt; endl;
      if (send(this-&gt;_socket, data, message.size(), 0) == INVALID_SOCKET)
      {
          throw std::exception("Error while sending message to client");
      }
  }
  </code></pre>
  
  <p>And on the (Java) application side: (Don't worry about startOperation, it just handles the networking magic, also, client._fromServer.firstElement() is the image string sent from the server)</p>
  
  <pre><code>case R.id.translate_word:
                  if (startOperation("SendPicture"))
                  {
                      byte[] byteArray = Base64.getDecoder().decode(client._fromServer.firstElement());
                      Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
                      ImageView image = (ImageView) findViewById(R.id.sign);
  
                      image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));
                      client._success = false;
                  }
  </code></pre>
  
  <p>The server crashes when the client asks for the picture, and it does on the Base64::Encode line. I don't really know how that works either and I'm sorry for being a noob overall :P
  So, I wanted to ask if all this is even needed? is there an easier way of doing this? am I even doing this right? could it be the base64 converter? Thanks a lot in advance &lt;3 Much love :)</p>
  
  <p>Edit: initialized the new_message string, now the server doesn't crash but nothing happens on the client side... it does get a response but nothing happens</p>
  
  <p>Edit2: I managed to send the message from the server properly (or at least I think so), the problem was the 0's in the binary file were read as null terminators. anyway, when I receive the message in the client only 28642 bytes are received (out of 30965). Does anyone know what's wrong? (also I gave up on the base64 encoding for now). Updated the sendImage code above. Rest are unchanged.</p>