title: translate a boost ASIO buffer data to an opencv matrix of JPEG format
body: <p>I've built a C++ program to send a single jpg frame (along with a metadata header beforehand) over a TCP socket.. my trouble is.. I am getting the bytes successfully.. parsing my header and seeing my dimenions.. the number of bytes... but my question is.. how do I take this data now.. and recreate my JPEG?</p>
  <p>I keep trying to cast the buffer but getting stopped out</p>
  <pre><code>std::cout &lt;&lt; &quot;[IMAGE_WIDTH]  = &quot; &lt;&lt; std::to_string(IMAGE_WIDTH) &lt;&lt; std::endl;
  std::cout &lt;&lt; &quot;[IMAGE_HEIGHT] = &quot; &lt;&lt; std::to_string(IMAGE_HEIGHT) &lt;&lt; std::endl;
  std::cout &lt;&lt; &quot;image bytes  = &quot; &lt;&lt; std::to_string(header_data.image_size_bytes) &lt;&lt; std::endl;
  
  Mat img = Mat::zeros(IMAGE_WIDTH, IMAGE_HEIGHT, CV_8UC3);
  const unsigned char* image_data_matrix = boost::asio::buffer_cast&lt;const unsigned char*&gt;(second_receive_buffer.data());
  img.data = image_data_matrix;
  imshow(&quot;server&quot;, img);
  waitKey(100);
  </code></pre>
  <p>error I get from <code>gcc</code></p>
  <pre><code>[ 50%] Building CXX object CMakeFiles/sockets-client.dir/src/client.cpp.o
  /bootstrap-cpp-sockets/client/src/client.cpp:81:28: error: assigning to 'uchar *' (aka 'unsigned char *') from 'const unsigned char *' discards qualifiers
                  img.data = image_data_matrix;
                             ^~~~~~~~~~~~~~~~~
  1 error generated.
  make[2]: *** [CMakeFiles/sockets-client.dir/src/client.cpp.o] Error 1
  make[1]: *** [CMakeFiles/sockets-client.dir/all] Error 2
  make: *** [all] Error 2
  </code></pre>
  <p><strong>Full Client.cpp</strong></p>
  <pre><code>int main()
  {
      try{
          boost::asio::io_service io_service;
          tcp::endpoint end_point(boost::asio::ip::address::from_string(&quot;127.0.0.1&quot;), 3200);
          tcp::socket socket(io_service);
          socket.connect(end_point);
          boost::system::error_code ignored_error;
          boost::asio::streambuf receive_buffer;
  
          // Now we retrieve the message header of 64 bytes
          size_t header_size = 64;
          boost::asio::read(socket, receive_buffer, boost::asio::transfer_exactly(header_size), ignored_error);
  
          if ( ignored_error &amp;&amp; ignored_error != boost::asio::error::eof ) {
              cout &lt;&lt; &quot;first receive failed: &quot; &lt;&lt; ignored_error.message() &lt;&lt; endl;
          } else {
              image_metadata_t header_data = parse_header(receive_buffer);
  
              const int IMAGE_WIDTH = header_data.width;
              const int IMAGE_HEIGHT = header_data.height;
  
              // Now we retrieve the frame itself
              boost::asio::streambuf second_receive_buffer;
              boost::asio::read(socket, second_receive_buffer, boost::asio::transfer_exactly(header_data.image_size_bytes), ignored_error);
              if( ignored_error &amp;&amp; ignored_error != boost::asio::error::eof ) {
                  cout &lt;&lt; &quot;SECOND receive failed: &quot; &lt;&lt; ignored_error.message() &lt;&lt; endl;
              }
              else {
                  std::cout &lt;&lt; &quot;[IMAGE_WIDTH]  = &quot; &lt;&lt; std::to_string(IMAGE_WIDTH) &lt;&lt; std::endl;
                  std::cout &lt;&lt; &quot;[IMAGE_HEIGHT] = &quot; &lt;&lt; std::to_string(IMAGE_HEIGHT) &lt;&lt; std::endl;
                  std::cout &lt;&lt; &quot;image bytes  = &quot; &lt;&lt; std::to_string(header_data.image_size_bytes) &lt;&lt; std::endl;
  
                  Mat img = Mat::zeros(IMAGE_WIDTH, IMAGE_HEIGHT, CV_8UC3);
                  const unsigned char* image_data_matrix = boost::asio::buffer_cast&lt;const unsigned char*&gt;(second_receive_buffer.data());
                  img.data = image_data_matrix;
                  imshow(&quot;server&quot;, img);
                  waitKey(100);
              }
          }
      } catch (std::exception&amp; e) {
          std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
      }
   
      return 0;
  }
  </code></pre>
  <p>the eventual finished program will keep reading the socket, receiving a header with the metadata.. and showing the next frame.. hopefully 30 Frames per second will be hit but.. first thing first I just want to prove i can do this with one frame.. and build up from there.</p>
  <p>Thanks !</p>