Results 1 to 3 of 3

Thread: how to use qtcpsocket send qimage data

  1. #1
    Join Date
    Apr 2009
    Posts
    35
    Thanks
    9
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Android

    Default how to use qtcpsocket send qimage data

    my tcpserver code
    Qt Code:
    1. void UserThread::sendData()
    2. {
    3. qDebug() << "sendData";
    4.  
    5. #ifdef TEST
    6. qDebug() << "TEST";
    7. QImage image("3.png");
    8.  
    9. qDebug() << image.byteCount() <<image.size();
    10. ba.append((char *)image.bits(),image.byteCount());
    11. qDebug() <<ba.size();
    12. this->m_tcpSocket->write(ba);
    13. if(!this->m_tcpSocket->waitForBytesWritten(-1))
    14. {
    15. qDebug() << "writen Bytes error " << this->m_tcpSocket->errorString();
    16. }
    17. this->m_tcpSocket->flush();
    18. #endif
    19. }
    To copy to clipboard, switch view to plain text mode 

    client code
    Qt Code:
    1. void MainWindow::readImage(){
    2. qDebug() << socket.bytesAvailable();
    3. ba = socket.readAll();
    4. socket.flush();
    5. qDebug() << ba.size();
    6. QImage image((uchar *)ba.data(),1024,768,QImage::Format_RGB32);
    7. ui->label->setPixmap(QPixmap::fromImage(image));
    8. }
    To copy to clipboard, switch view to plain text mode 

    my picture at attachment

    server read the picture sizse is 3145728

    but client recevie a wrong size
    i used flush
    but ...
    Attached Images Attached Images
    • File Type: jpg 3.jpg (69.1 KB, 38 views)

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to use qtcpsocket send qimage data

    server read the picture sizse is 3145728
    but client recevie a wrong size
    Try looping on the clinet side until your buffer gets filled with the correct size.
    Qt Code:
    1. //This is pseudo code
    2. while((size < imageSize) && !bTimeout)
    3. {
    4. ba.append(socket.readAll());
    5. }
    To copy to clipboard, switch view to plain text mode 

    you don't need to call flush() on the client side.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Aug 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to use qtcpsocket send qimage data

    server read the picture sizse is 3145728

    but client recevie a wrong size


    TCP will break up your bytearray into lots of different packets which you must append to the socket bytearray... you need them all for the complete image. (readAll() only reads all of the first packet).

Similar Threads

  1. Replies: 4
    Last Post: 22nd March 2010, 18:32
  2. Send QImage over network
    By winston2020 in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2009, 19:09
  3. How to send Pixmap through QTcpSocket?
    By live_07 in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2008, 16:35
  4. Send Base64 encoded data
    By rmagro in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 16:58
  5. My client can't send data
    By hiuao in forum Qt Programming
    Replies: 10
    Last Post: 23rd February 2007, 09: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.