title: sending Iplimage using sockets
body: <p>I am trying to capture frames from a Logitech HD cam connected to a Raspberry Pi through usb, the RP is running arch linux and I am using OpenCV C api and a TCP client.</p>
  
  <p>The TCP server is running c++(QT) under ubuntu.</p>
  
  <p>I am storing the frame->imageData in a buffer and then send the data in the buffer.</p>
  
  <p>this is a proof of concept that I can save a frame to a buffer then reconstruct a new frame which is tmpbuffer from the buffer,actually this code is running perfectly:</p>
  
  <pre><code>CvCapture *capture = cvCaptureFromCAM(1);
  cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
      int i =1;
  
     while ( 1 ) {
       // Get one frame
       IplImage* frame = cvQueryFrame( capture );
       CvSize size;
       size.height = 480;
    size.width = 640;
       IplImage* tmpframe = cvCreateImageHeader(size, IPL_DEPTH_8U, 3); //create a new frame
       if ( !frame ) {
         //fprintf( stderr, "ERROR: frame is null...\n" );
         getchar();
         break;
       }else
       {
           //std::cout&lt;&lt;i&lt;&lt;std::endl;
           char buffer[frame-&gt;imageSize];
           snprintf(buffer,frame-&gt;imageSize,"%s",frame-&gt;imageData);
           //printf("frame-&gt;height= %s\n",buffer);
  
           tmpframe-&gt;imageData = buffer;
           printf("data %s\n",tmpframe-&gt;imageData);
  
           //snprintf(IDbuffer,10,"%d",frame-&gt;ID);
           //printf("frame-&gt;ID= %s\n",IDbuffer);
           i++;
       }
       cvShowImage( "mywindow", tmpframe );
  </code></pre>
  
  <p>Here is my client.c file:</p>
  
  <pre><code>#include &lt;opencv/cv.h&gt;
   #include &lt;opencv/highgui.h&gt;
   #include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  
  
  void error(char *msg)
  {
   perror(msg);
   exit(0);
  }
  
  int main(int argc,char *argv[])
  {
  
  int sockfd,portno,n;
   struct sockaddr_in serv_addr;
   struct hostent *server;
  
  
   if(argc &lt;3)
   {
    fprintf(stderr,"usage %s hostname portname port\n",argv[0]);
    exit(0);
   }
  
   portno = atoi(argv[2]);
   sockfd = socket(AF_INET , SOCK_STREAM,0);
  
   if(sockfd &lt; 0)
   {
    error("ERROR OPENING SOCKET");
   }
  
   server = gethostbyname(argv[1]);
   if(server == NULL)
   {
    fprintf(stderr,"ERROR,NO SUCH HOST\n");
    exit(0);
   }
  
   bzero((char*)&amp;serv_addr,sizeof(serv_addr));
   serv_addr.sin_family = AF_INET;
  
   bcopy((char*)server-&gt;h_addr,(char*)&amp;serv_addr.sin_addr.s_addr,server-&gt;h_length);
   serv_addr.sin_port = htons(portno);
  
   if(connect(sockfd,&amp;serv_addr,sizeof(serv_addr)) &lt; 0)
   {
    error("ERROR CONNECTING");
   }
      CvCapture *capture = cvCaptureFromCAM(1);
         // Show the image captured from the camera in the window and repeat
          int i =1;
  
         while ( 1 ) {
           // Get one frame
           IplImage* frame = cvQueryFrame( capture );
           if ( !frame ) {
             //fprintf( stderr, "ERROR: frame is null...\n" );
             getchar();
             break;
           }else
           {
               i++;
           }
  
           char *msg = frame-&gt;imageData;
           char buffer[frame-&gt;imageSize];
           bzero (buffer,frame-&gt;imageSize);
           snprintf(buffer,frame-&gt;imageSize,"%s",msg);
   n=write(sockfd,buffer,frame-&gt;imageSize);
   if(n &lt;0)
   {
    error("ERROR READING FROM SOCKET");
   }
   //printf("%s\n",buffer);
  
  
   }
  return 0;
  }
  </code></pre>
  
  <p>and this is how I am receiving data on my server:</p>
  
  <pre><code>void HostConnector::readyRead()
  {
      QByteArray Data = socket-&gt;readAll();
  
      CvSize size;
      size.height = 480;
      size.width = 640;
      IplImage *frame = cvCreateImageHeader(size, IPL_DEPTH_8U, 3);
  
  
      frame-&gt;imageData = Data.data();
      cvShowImage( "mywindow", frame );
  
  }
  </code></pre>
  
  <p>But the server is crashing !!
  a <code>segmentation fault</code> at</p>
  
  <pre><code>cvShowImage( "mywindow", frame );
  </code></pre>