title: OpenCV Camera UDP socket stream
body: <p>I'm trying to code a client that captures raw Frames from the camera and sends these frames to the server over an udp socket.</p>
  
  <p>Here is the Code from my Client:</p>
  
  <pre><code>cv::VideoCapture cap(0); // open the video camera no. 0
  if (!cap.isOpened()){
      puts("Cannot open the video cam");
      return -1;
  }
  
  double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
  double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
  cout &lt;&lt; "Frame size : " &lt;&lt; dWidth &lt;&lt; " x " &lt;&lt; dHeight &lt;&lt; endl;
  
  while (TRUE){
      cv::Mat frame;
  
      bool bSuccess = cap.read(frame); // read a new frame from video
      if (!bSuccess){
          puts("Cannot read a frame from video stream");
          break;
      }
  
      frame = (frame.reshape(0, 1)); // to make it continuous
      int  imgSize = frame.total()*frame.elemSize();
  
      if (send(s, (const char*)frame.data, imgSize, 0) &lt; 0){
          printf("Send failed %d\n", GetLastError());
          return -1;
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the server:</p>
  
  <pre><code>DWORD bytes = 0, imgSize = 0;
  while (TRUE){
  
      recv(new_socket, (char*)buffer, 5999, 0);
      cv::Mat  img = cv::Mat::zeros(690, 690, CV_8UC3);
      imgSize = img.total()*img.elemSize();
      //Receive data here
      for (int i = 0; i &lt; imgSize; i += bytes) {
          if ((bytes = recv(new_socket, (char*)sockData + i, imgSize - i, 0)) == -1) {
              puts("recv failed");
              break;
          }
      }
  
      // Assign pixel value to img
      int ptr = 0;
      for (int i = 0; i &lt; img.rows; i++) {
          for (int j = 0; j &lt; img.cols; j++) {
                      // following line crashes with Access violation
              img.at&lt;cv::Vec3b&gt;(i, j) = cv::Vec3b(sockData[ptr + 0], sockData[ptr + 1], sockData[ptr + 2]);               ptr = ptr + 3;
          }
      }
  
      cv::imwrite("prova.jpg", img);
      cv::namedWindow("Server", CV_WINDOW_AUTOSIZE);  //Create a Window for display
      cv::imshow("Server", img);
  }
  </code></pre>
  
  <p>Using this code, I get an Access Violation on the server.
  What am i doing wrong?</p>
  
  <p>Can someone provide an working example?</p>