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;
file1.open(QIODevice::ReadOnly);
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"); file.resize(0); file.open(QIODevice::Append); QTextStream out(&file); QByteArray data; data.resize(socket->pendingDatagramSize()); socket->readDatagram(data.data(),data.size()); 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
-
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");
writer.write(img);
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.