Hey guys,

First up I'm new to this whole Qt-programming, but it's been a fun challenge thus far.

What I'm trying to do is send a QImage across the network using a QTcpSocket. Let's for a second pretend I don't care about bandwidth, I just want to try and get this working.

Below is my server code (snippet):

Qt Code:
  1. QBuffer image_buffer;
  2. QImageWriter writer(&image_buffer, "PNG");
  3. if(!writer.write(captured_image.getImage()))
  4. qWarning()<<"ERROR: Unable to write image to buffer: "<<writer.errorString();
  5.  
  6. QDataStream out(&data, QIODevice::WriteOnly);
  7. out.setVersion(QDataStream::Qt_4_0);
  8. out<<(quint32)image_buffer.data().size();
  9.  
  10. qDebug()<<"Image size = "<<image_buffer.data().size();
  11.  
  12. out<<image_buffer.data();
  13.  
  14. video_socket.write(data);
  15. if(!video_socket.waitForBytesWritten())
  16. qWarning()<<"ERROR: Unable to send image: "<<video_socket.errorString();
To copy to clipboard, switch view to plain text mode 

My client code (snippet) is as follows:

Qt Code:
  1. QDataStream in(&video_socket);
  2. in.setVersion(QDataStream::Qt_4_0);
  3.  
  4. while(video_socket.bytesAvailable() < sizeof(quint32))
  5. {
  6. qDebug()<<"Waiting for image size...";
  7. if(!video_socket.waitForReadyRead())
  8. {
  9. qWarning()<<"ERROR: Failed to read image size: "<<video_socket.errorString();
  10. return;
  11. }
  12. }
  13.  
  14. quint32 image_size;
  15. in>>image_size;
  16.  
  17. qDebug()<<"Image Size = "<<image_size;
  18.  
  19. while(video_socket.bytesAvailable() < image_size)
  20. {
  21. qDebug()<<"Waiting for image...";
  22. if(!video_socket.waitForReadyRead())
  23. {
  24. qWarning()<<"ERROR: Failed to read image: "<<video_socket.errorString();
  25. return;
  26. }
  27. }
  28.  
  29. in>>data;
  30.  
  31. qDebug()<<"Data size = "<<data.size();
  32.  
  33. QBuffer image_buffer(&data);
  34. QImageReader reader(&image_buffer, "PNG");
  35. QImage received_image;
  36.  
  37. if(!reader.read(&received_image))
  38. {
  39. qWarning()<<"ERROR: Unable to read received image from buffer: "<<reader.errorString();
  40. return;
  41. }
To copy to clipboard, switch view to plain text mode 

However, the client only seems to be able to read the size of the image, then dies.

Below is the output from the qDebug() statements in the server:

Image size = 384387
Data written = 384395

And below is my output from qDebug() statements in the client program:

Waiting for image size...
Image Size = 384387
Waiting for image...
Waiting for image...
Waiting for image...
ERROR: Failed to read image: "The remote host closed the connection"

So I guess my question is, can anyone see why it is doing this? I'm at a loss...

Any help would be terrific!!!

Regards,
Adrian