Results 1 to 5 of 5

Thread: Receive varialbe length Image Problem??

  1. #1
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Receive varialbe length Image Problem??

    The following code is intended to display an Image sent over network. I sent a header of 16 bytes which I use to calculate the size of image that follows and then read that many bytes and display the image.
    Qt Code:
    1. void socket::readyRead()
    2. {
    3.  
    4. while(socket->bytesAvailable() > 0) {
    5. quint8 Data[16];
    6.  
    7. socket->read((char *)&Data,16);
    8. img_size = (((quint8)Data[1]<<8)+ (quint8)Data[0]) * (((quint8)Data[3]<<8)+ (quint8)Data[2]) * 1;
    9. QByteArray buffer = socket->read(img_size);
    10.  
    11. qDebug() << "image zi" << img_size+1;
    12.  
    13. while(buffer.size() < (img_size))
    14. {
    15. // qDebug() << buffer.size();
    16. socket->waitForReadyRead();
    17. buffer.append(socket->read((img_size)-(buffer.size()) ));
    18. }
    19. unsigned char* imgdatara = (unsigned char*)&buffer.data()[0];
    20. if( !image )
    21. image = new QImage(imgdatara,32,640,QImage::Format_Grayscale8);
    22. else
    23. {
    24. delete image;
    25. image = new QImage(imgdatara,32,640,QImage::Format_Grayscale8);
    26. }
    27.  
    28.  
    29. emit msg(image);
    30.  
    31.  
    32. }
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 
    My GUI says "not responding" and freezes.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Receive varialbe length Image Problem??

    First of all when you go into socket::readyRead() method there is no guarantee that it received 16 bytes (full header). It may be 1 byte or 1000 bytes.

  3. #3
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Receive varialbe length Image Problem??

    Quote Originally Posted by Lesiok View Post
    First of all when you go into socket::readyRead() method there is no guarantee that it received 16 bytes (full header). It may be 1 byte or 1000 bytes.
    Ok Lesiok....

    I made the following changes:

    Qt Code:
    1. void socket::readyRead()
    2. {
    3.  
    4. while(socket->bytesAvailable() > 0) {
    5.  
    6. QByteArray Data;
    7. QByteArray buffer
    8.  
    9. while(socket->bytesAvailable < 16) //// I wait til 16 bytes header arrives and then do socket->read(16)
    10. {
    11. socket->waitForReadyRead();
    12. }
    13. Data = socket->read(16);
    14.  
    15. img_size = (((quint8)Data[1]<<8)+ (quint8)Data[0]) * (((quint8)Data[3]<<8)+ (quint8)Data[2]) * 1;
    16.  
    17.  
    18. while(buffer.size() < (img_size))
    19. {
    20.  
    21. socket->waitForReadyRead();
    22. buffer.append(socket->read((img_size)-(buffer.size()) ));
    23. }
    24. unsigned char* imgdatara = (unsigned char*)&buffer.data()[0];
    25. if( !image )
    26. image = new QImage(imgdatara,32,640,QImage::Format_Grayscale8);
    27. else
    28. {
    29. delete image;
    30. image = new QImage(imgdatara,32,640,QImage::Format_Grayscale8);
    31. }
    32.  
    33.  
    34. emit msg(image);
    35.  
    36.  
    37. }
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

    This code still does not work.I am unable to find what is wrong even though I do understand how TCP receives message.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Receive varialbe length Image Problem??

    Hi,

    First, use a buffer to store the data from the socket.

    Into "readyRead()" method, first check if you have almost 16 bytes into the socket.
    If you have 16 bytes into the socket you can read all available bytes and store it into your buffer.
    After this you can check if the buffer size have the bytes that your header points to plus 16 bytes(header). If not, you will receive another "readyRead" signal (it's like a loop).
    When the buffer contains all the bytes you can create the image and don't forget to clear the buffer for the next receiving image.
    Òscar Llarch i Galán

  5. The following user says thank you to ^NyAw^ for this useful post:

    ganapathi (11th February 2016)

  6. #5
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Receive varialbe length Image Problem??

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    First, use a buffer to store the data from the socket.

    Into "readyRead()" method, first check if you have almost 16 bytes into the socket.
    If you have 16 bytes into the socket you can read all available bytes and store it into your buffer.
    After this you can check if the buffer size have the bytes that your header points to plus 16 bytes(header). If not, you will receive another "readyRead" signal (it's like a loop).
    When the buffer contains all the bytes you can create the image and don't forget to clear the buffer for the next receiving image.

    Yes this worked ....thanks a lot..

Similar Threads

  1. Problem with tcp - client receive message
    By gelman in forum Newbie
    Replies: 3
    Last Post: 25th August 2011, 14:35
  2. udp receive data problem
    By zxwmail in forum Newbie
    Replies: 9
    Last Post: 21st June 2011, 22:11
  3. Replies: 4
    Last Post: 18th August 2010, 08:13
  4. Replies: 1
    Last Post: 30th November 2007, 10:03

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.