Qt World Summit: Submit your Presentation

Transferring images over qudpsocket in qt

  • Hi,
    I am trying to send an image file over qudpsocket.The code I used to send a cpp file is given below.8388 is the size of the file.
    @ QFile file1("/home/test.cpp");
    QByteArray bytes;
    QTextStream in(&file1);
    bytes = in.read(8838).toLatin1();
    socket->writeDatagram(bytes, QHostAddress::Broadcast,9988);@

    and I received the file using the code give below.

           @QFile file("received.txt");
           QTextStream out(&file);
                   QByteArray data;
                   QString str=data.data();
                    out << data;@

    How ever when I transmit an jpeg file like this.How to get the image file to Qdatastream and send like this easily and receive at the client end..Pleas help

  • Qt Champions 2017

    It all about serializing your binary file in to byte-array and passing it.
    Try this

    @QImage img=QImage("my.JPG");
    QBuffer buffer;
    QImageWriter writer(&buffer, "JPQ");
    QByteArray data;
    data.append( buffer.data() );
    socket->writeDatagram(data, QHostAddress::Broadcast,2000);@

    At service side also use the QDataStream to reach appropriately. I have not tried.

  • thanks a lot.That was what I am searching for.

Log in to reply