title: c++, boost: how to fill buffer and transfer (image) file data over network?
body: <p>I'm working on an application where I need to send a "filename", "filesize" and the filedata over the network. I created a server using boost which, for now, reads in the filesize and name. </p>
  
  <p>I'm wondering how I can fill a buffer with the file data (if necessary) and how to transfer it to the server. </p>
  
  <p>This is what I've got now:</p>
  
  <pre><code>#ifndef OFXFILETRANSFERSENDH
  #define OFXFILETRANSFERSENDH
  
  #undef check // necessary to get Boost running on Mac
  
  #include &lt;vector&gt; 
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/enable_shared_from_this.hpp&gt;
  
  #include "ofxFileTransferConnection.h"
  using boost::asio::ip::tcp;
  class ofxFileTransferSend : public boost::enable_shared_from_this&lt;connection&gt; {
  public:
      typedef boost::shared_ptr&lt;ofxFileTransferSend&gt; pointer;
      static pointer create(
                          boost::asio::io_service&amp; rIOService
                          ,std::string sServer
                          ,const char* sPort) 
      {
          return pointer(new ofxFileTransferSend(rIOService,sServer, sPort));
      }
  
  
  private:
      //--------------------------------------------------------------
      ofxFileTransferSend(
                      boost::asio::io_service &amp;rIOService
                      ,const std::string sServer
                      ,const char* sPort
      )
          :port(sPort)
          ,socket_(rIOService)
          ,resolver_(rIOService)
      {
  
          tcp::resolver::query query(sServer, sPort);
          resolver_.async_resolve(
                              query
                              ,boost::bind(
                                      &amp;ofxFileTransferSend::handleResolve
                                      ,this
                                      ,boost::asio::placeholders::error
                                      ,boost::asio::placeholders::iterator
                              )
          );
      }
  
  
      //--------------------------------------------------------------
      void handleResolve(
          const boost::system::error_code &amp;rError
          ,tcp::resolver::iterator oEndPointIterator
      )
      {
          if (!rError) {
              tcp::endpoint end_point = *oEndPointIterator;
              socket_.async_connect(
                                  end_point
                                  ,boost::bind(
                                      &amp;ofxFileTransferSend::handleConnect
                                      ,this
                                      ,boost::asio::placeholders::error
                                      ,++oEndPointIterator
                                  )
              );
          }
          else {
              std::cout &lt;&lt; "Error while resolving server: " &lt;&lt; std::endl;
          }
      }
  
      //--------------------------------------------------------------
      void handleConnect(
          const boost::system::error_code &amp;rError
          ,tcp::resolver::iterator rOEndPointIterator
      )
      {
          if(!rError) {
              std::cout &lt;&lt; "Connected to remote server!" &lt;&lt; std::endl;
              std::size_t size = 1235;
              std::ostream send_data_stream(&amp;send_data);
              send_data_stream &lt;&lt; "filename.jpg" &lt;&lt; "\r\n";
              send_data_stream &lt;&lt; size &lt;&lt; "\r\n";
  
              boost::asio::async_write(
                              socket_
                              ,send_data
                              ,boost::bind(
                                  &amp;ofxFileTransferSend::handleSendFileInfo
                                  ,this
                                  ,boost::asio::placeholders::error
                              )
              );
          }
          else {
              // @todo on failure retry!
              std::cout &lt;&lt; "Error connecting to ofxFileTransferServer:" &lt;&lt; rError.message()&lt;&lt; std::endl;
          }
      }
  
  
      //--------------------------------------------------------------
      void handleSendFileInfo(
          const boost::system::error_code &amp;rError
      )
      {
          if(!rError) {
              cout &lt;&lt; "okay nice, send file data done!\n";
          }
          else {
              std::cout &lt;&lt; "Error sending file info: " &lt;&lt; rError.message() &lt;&lt; std::endl;
          }
      }   
  
      tcp::resolver resolver_;
      tcp::socket socket_;
      boost::asio::streambuf send_data;
      const char* port;
  };
  #endif
  </code></pre>