title: Boost asio: Send OpenCV IplImage from Ubuntu-Server to Win7-Client
body: <p>I try to transmit an OpenCV IplImage from a Server (Ubuntu x64) to a Client (Win7 x64) using the boost asio library.</p>
  
  <p>The following code works fine if both (Client and Server) are on the same operating system. But when the server is on Ubuntu and the client on Win7 it doesn't work. The image header is correct, but something with the image data is wrong.</p>
  
  <p>I think this is because of the different bit-order between the two OS.  Is it? How can I resolve this problem?</p>
  
  <p>And second: The transmission with this code is very slow.
  How can I improve the speed?</p>
  
  <p>Client:</p>
  
  <pre><code>#define _WIN32_WINNT 0x0601 
  
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;boost/array.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;cv.h&gt;
  #include &lt;cxcore.h&gt;
  #include &lt;highgui.h&gt;
  
  using boost::asio::ip::tcp;
  
  using namespace std;
  
  char* splitImage(const string&amp; str, const string&amp; delim, vector&lt;string&gt;&amp; parts) {
    size_t start, end = 0;
    int i = 0;
    while (end &lt; str.size() &amp;&amp; i &lt; 8) {
      start = end;
      while (start &lt; str.size() &amp;&amp; (delim.find(str[start]) != string::npos)) {
        start++;  // skip initial whitespace
      }
      end = start;
      while (end &lt; str.size() &amp;&amp; (delim.find(str[end]) == string::npos)) {
        end++; // skip to end of word
      }
      if (end-start != 0) {  // just ignore zero-length strings.
        parts.push_back(string(str, start, end-start));
      }
      i++;
    }
    int size = atoi(parts[6].c_str());
    char *imgdata = new char[size];
    memcpy(imgdata, string(str, end+1, size).c_str(), size);
    return imgdata;
  
  }
  
  int main(int argc, char* argv[])
  {
  
    string header;
    try
    {
  
      boost::asio::io_service io_service;
  
      tcp::resolver resolver(io_service);
      tcp::resolver::query query("localhost", "1234");
      tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
      tcp::resolver::iterator end;
  
      tcp::socket socket(io_service);
      boost::system::error_code error = boost::asio::error::host_not_found;
      while (error &amp;&amp; endpoint_iterator != end)
      {
        socket.close();
        socket.connect(*endpoint_iterator++, error);
      }
      if (error)
        throw boost::system::system_error(error);
  
      stringstream ss;
  
      cout &lt;&lt; "reading";
      for (;;)
      {
        boost::array&lt;char, 1024&gt; buf;
        boost::system::error_code error;
  
        size_t len = socket.read_some(boost::asio::buffer(buf), error);
        cout &lt;&lt; ".";
  
        if (error == boost::asio::error::eof)
          break; // Connection closed cleanly by peer.
        else if (error)
          throw boost::system::system_error(error); // Some other error.
  
        ss.write(buf.data(), len);
        header = ss.str();
      }
      cout &lt;&lt; "done: data size: "&lt;&lt; header.size() &lt;&lt; endl;
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
      return 1;
    }
  
    vector&lt;string&gt; parts; 
    char* imgdata = splitImage(header,"#",parts); 
  
    IplImage img2;    
    img2.nSize = atoi(parts[0].c_str());  
    img2.ID = 0;  
    img2.nChannels = atoi(parts[1].c_str());  
    img2.depth = atoi(parts[2].c_str());  
    img2.dataOrder = atoi(parts[3].c_str());; 
    img2.height = atoi(parts[4].c_str()); 
    img2.width = atoi(parts[5].c_str());  
    img2.roi = NULL;  
    img2.maskROI = NULL;  
    img2.imageId = NULL;  
    img2.tileInfo = NULL; 
    img2.imageSize = atoi(parts[6].c_str());      
    img2.widthStep = atoi(parts[7].c_str());  
    img2.imageData = imgdata;
  
    cvNamedWindow("Image:",1);  
    cvShowImage("Image:",&amp;img2);
    cvWaitKey();  
    cvDestroyWindow("Image:");
  
    delete[] imgdata;
  
    return 0;
  }
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>#define _WIN32_WINNT 0x0601 
  #define WIN32_LEAN_AND_MEAN 
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  #include &lt;cv.h&gt;
  #include &lt;cxcore.h&gt;
  #include &lt;highgui.h&gt;
  #include &lt;boost/asio.hpp&gt;
  
  #define DELIMITER "#"
  
  using boost::asio::ip::tcp;
  using namespace std;
  
  IplImage *img;
  
  string serializeImageHeader(){
      stringstream ss;
      ss &lt;&lt; img-&gt;nSize &lt;&lt; DELIMITER;
      ss &lt;&lt; img-&gt;nChannels &lt;&lt; DELIMITER;
      ss &lt;&lt; img-&gt;depth &lt;&lt; DELIMITER;
      ss &lt;&lt; img-&gt;dataOrder &lt;&lt; DELIMITER;
      ss &lt;&lt; img-&gt;height &lt;&lt; DELIMITER;
      ss &lt;&lt; img-&gt;width &lt;&lt; DELIMITER;
      ss &lt;&lt; img-&gt;imageSize &lt;&lt; DELIMITER;
      ss &lt;&lt; img-&gt;widthStep &lt;&lt; DELIMITER;
  
      return ss.str();
  }
  
  
  int main()
  {
    try
    {
      boost::asio::io_service io_service;
  
      tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1234));
  
      img = cvLoadImage("Test.bmp");
      cout &lt;&lt; "Server is running" &lt;&lt; endl;
  
      for (;;)
      {
        tcp::socket socket(io_service);
        acceptor.accept(socket);
        cout &lt;&lt; "socket accepted" &lt;&lt; endl;
  
        string message  = serializeImageHeader().append(img-&gt;imageData, img-&gt;imageSize);
  
        boost::system::error_code ignored_error;
        boost::asio::write(socket, boost::asio::buffer(message),
            boost::asio::transfer_all(), ignored_error);
        cout &lt;&lt; "sent: size: "&lt;&lt; message.size() &lt;&lt; endl;
      }
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
    }
  
    cvReleaseImage(&amp;img);
  
    return 0;
  }
  </code></pre>