title: OpenCV C++ Convert Byte array to Mat
body: <p>How can I convert byte array  to Mat which is received from socket ?.</p>
  
  <p>My client application will send color image data like this</p>
  
  <pre><code>Mat frame; //colour image
  int imgSize = frame.total()*frame.elemSize();
  int bytes = send(clientSock, frame.data, imgSize, 0));//write to the socket
  </code></pre>
  
  <p>And the server will receives the data like</p>
  
  <pre><code>    char sockData[imgSize];
      Mat img;
      for (int i = 0; i &lt; imgSize; i += bytes) {
          bytes = recv(connectSock, sockData +i, imgSize  - i, 0));
      }
  
     // Write to mat
      for (int i = 0;  i &lt; img.rows; i++) {
        for (int j = 0; j &lt; img.cols; j++) {
           (img.row(i)).col(j) = (uchar)sockData[((img.cols)*i)+j];
         }
       }
  </code></pre>
  
  <p>I am getting distorted image at the receiver. Is there any problem in my code ?
  Thanks in advance.......</p>