Results 1 to 6 of 6

Thread: Errors when reading a QImage sent through a socket

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Errors when reading a QImage sent through a socket

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Errors when reading a QImage sent through a socket

    You're overcomplicating things. If you are using QDataStream then just stream the image directly into it.

    Qt Code:
    1. QImage img;
    2. QDataStream str(&socket);
    3. str << img;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QImage img;
    2. QDataStream str(&socket);
    3. str >> img;
    To copy to clipboard, switch view to plain text mode 

    Currently you are misusing the data stream, it is not a general purpose binary stream but a serialization mechanism - it adds additional information to data it is being passed to, so you can't write a byte array directly to a socket and on the other end stream it out using QDataStream as the data lacks that additional information QDataStream requires.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Errors when reading a QImage sent through a socket

    Thanks very much wysota.

    I changed my client code to the following:

    Qt Code:
    1. // Block until data is available for reading
    2. if(!video_socket.waitForReadyRead())
    3. {
    4. qWarning()<<"ERROR: could not read image: "<<video_socket.errorString();
    5. return;
    6. }
    7.  
    8. // Read the image from the socket via the data stream
    9. in>>received_image;
    10.  
    11. qDebug()<<"Image received";
    To copy to clipboard, switch view to plain text mode 

    And server to:

    Qt Code:
    1. // Write the image to the socket via the data stream
    2. out<<captured_image.getImage();
    3.  
    4. // Block until the data stream has been written to the socket
    5. if(!video_socket.waitForBytesWritten())
    6. qWarning()<<"ERROR: Unable to send image: "<<video_socket.errorString();
    7.  
    8. qDebug()<<"Image sent";
    To copy to clipboard, switch view to plain text mode 

    And for some images it works perfectly.

    However, for other images (seems to be the larger the image the more likely it is to happen), I get the following message:

    libpng error: Read Error

    A bit of googling shows this may be a Linux problem rather than a problem with what I am doing in Qt. However, I havent managed to find a reason for it yet. I've tried different QDataStream versions (Qt_4_0 to Qt_4_5) but the error stil occurs. My libpng (Ubuntu distro) seems to be all up to date (at least from the repository anyway). Hmmm....

  4. #4
    Join Date
    Sep 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Errors when reading a QImage sent through a socket

    Hi,

    I got the same problem also on Ubuntu (januty)

    If anyone has an idea about how to overcome this issue, it would be most appreciated.

    Ciao
    joergwl

  5. #5
    Join Date
    Sep 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Errors when reading a QImage sent through a socket

    SOLVED

    I used the fortune samples for a start and the block length is defined as quint16, which is a bit to small to send bigger images.

    After changing the block length to qint64 all works fine

    joergwl

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Errors when reading a QImage sent through a socket

    The same problem meaning what?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Socket Reading causes CPU to max out
    By tntcoda in forum Qt Programming
    Replies: 3
    Last Post: 25th May 2009, 00:07
  2. Socket Reading Advice
    By tntcoda in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2008, 11:26
  3. reading writing qimage / qbitmap to and from memory
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2008, 11:28
  4. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  5. Reading from TCP Socket crashes program
    By OnionRingOfDoom in forum Qt Programming
    Replies: 26
    Last Post: 27th January 2006, 19:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.