0

I have two mac apps that communicate with each other using QLocalSocket. Able to send the received QString but not able to send the received QImage Below is my code.

SERVER SIDE CODE

 QImage image(":/asset/logo_active.png");
 QByteArray ba;
 qDebug() << image.sizeInBytes() <<image.size();
 ba.append((char *)image.bits(),image.sizeInBytes());
 qDebug() <<ba.size(); //262144
 this->mSocket->write(ba);
 if(!this->mSocket->waitForBytesWritten(-1))
 {
     qDebug() << "writen Bytes error " << this->mSocket->errorString();
 }
 this->mSocket->flush();

CLIENT SIDE CODE

    connect(mLocalSocket,&QLocalSocket::readyRead, [&]() {
        QByteArray ba;
        ba = mLocalSocket->readAll();
qDebug() << "size is" << ba.size(); // size is 0
        QImage image((uchar *)ba.data(),1024,768,QImage::Format_RGB32);
        ui->labelStream->setPixmap(QPixmap::fromImage(img));
    });

at sender 262144 is the byte-array size but at the receiver, byte-array size is 0

Do let me know if I am missing anything.

Thanks In Advance

Mohammed Ebrahim
  • 849
  • 1
  • 12
  • 22
  • 1
    Needs a little more context... Is the client side in a slot connected to the socket's readyRead() signal? If not, you'll likely need to either move it into such a slot, or call waitForReadyRead() to perform a blocking wait until data is available in the socket. – Zrax Apr 05 '22 at 17:57
  • @Zrax connect(mLocalSocket,&QLocalSocket::readyRead, [&]() {} – Mohammed Ebrahim Apr 05 '22 at 18:04
  • @Zrax I have update client code – Mohammed Ebrahim Apr 05 '22 at 18:07
  • What makes you certain the received `QByteArray` has zero size? The code shown doesn't check. – G.M. Apr 05 '22 at 18:39
  • @G.M. please check post again updated the client code – Mohammed Ebrahim Apr 06 '22 at 05:35
  • @MohammedEbrahim 1. The write method returns the number of bytes sent, and if something fails it returns -1, have you checked? 2. An image contains many bytes to be sent all at once, so it will most likely be sent in parts. When sending 1 image, how many times is the slot associated with readyRead called? – eyllanesc Apr 06 '22 at 05:50
  • @MohammedEbrahim The best thing is that you compress the image (for example jpg or use another format such as base64) and if the data is small then it will send everything, otherwise first send the size in bytes of the image and in others you send small packets and use the size sent to know when the data was completely sent. – eyllanesc Apr 06 '22 at 05:50
  • @eyllanesc 32 times. – Mohammed Ebrahim Apr 06 '22 at 07:20

1 Answers1

0

Finally I got the solutions I used QDataStream below is the code example.

SERVER SIDE CODE:

 QDataStream T(mSocket);
 T.setVersion(QDataStream::Qt_5_7);
 QByteArray ba;
 ba.append((char *)img.bits(),img.sizeInBytes());
 T << ba;
 mSocket->flush();

CLIENT SIDE CODE

QByteArray jsonData;
QDataStream socketStream(mLocalSocket);
socketStream.setVersion(QDataStream::Qt_5_7);

for (;;) {
 socketStream.startTransaction();
 socketStream >> jsonData;
 if (socketStream.commitTransaction()) {
  QImage image((uchar *)jsonData.data(),640,480,QImage::Format_RGB888);
  ui->labelStream->setPixmap(QPixmap::fromImage(image));
 }else {
                  // the read failed, the socket goes automatically back to the state it was in before the transaction started
                  // we just exit the loop and wait for more data to become available
                  break;
  }
}

Thanks, Everyone for your support also Stackoverflow.

Mohammed Ebrahim
  • 849
  • 1
  • 12
  • 22