Send/Receive QImage with QTcpSocket











up vote
0
down vote

favorite












I'm working on a simple Qt application for video streaming over TCP. There is a device (server side) that start listening for incoming connections. When a new connection is created, the server start to send images to the client. On the other side, the client receive and display the images.
I've read a lot about IPC on Qt, datastream and transactions, but I'm yet struggling with reading the incoming data. I know that TCP divide a long block in several packets, that's the point I'm in trouble.



Here is my code for sending ..



void CameraServer::sendData(const QImage &img)
{
if (mTcpClient->state() == QTcpSocket::ConnectedState) {

QByteArray imgArray = QByteArray::fromRawData((const char*) img.bits(), img.byteCount());

// init
QByteArray data;
QDataStream outStream(&data, QIODevice::WriteOnly);
outStream.setDevice(mTcpClient);
outStream.setVersion(QDataStream::Qt_5_11);

// serialize
outStream << imgArray.size();
outStream << imgArray;

// send
mTcpClient.write(data);
mTcpClient->waitForBytesWritten();
mTcpClient->flush();

qDebug() << "Sent" << data.size() << "bytes";
}


.. and for reading.



void VCameraClient::readData()
{
qDebug() << "Received" << mTcpSocket->bytesAvailable() << "bytes";


if (size == 0) {
// read the image size
// toBeRead is a class private member
inputStream >> toBeRead;
}

// read partial data
// byteArr is a class private member
if (mTcpSocket->bytesAvailable() < toBeRead) {
byteArr.append(mTcpSocket->read(mTcpSocket->bytesAvailable()));

// update the size to be read
toBeRead -= byteArr.size();
}

// read completed
if (toBeRead <= 0) {
mframe = QImage((const uchar*) byteArr.data(), 640, 480, QImage::FormatRGB888);
emit newFrameRead(mFrame);
}
}


Could anyone help me, please? How to use QDataStream transactions properly?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm working on a simple Qt application for video streaming over TCP. There is a device (server side) that start listening for incoming connections. When a new connection is created, the server start to send images to the client. On the other side, the client receive and display the images.
    I've read a lot about IPC on Qt, datastream and transactions, but I'm yet struggling with reading the incoming data. I know that TCP divide a long block in several packets, that's the point I'm in trouble.



    Here is my code for sending ..



    void CameraServer::sendData(const QImage &img)
    {
    if (mTcpClient->state() == QTcpSocket::ConnectedState) {

    QByteArray imgArray = QByteArray::fromRawData((const char*) img.bits(), img.byteCount());

    // init
    QByteArray data;
    QDataStream outStream(&data, QIODevice::WriteOnly);
    outStream.setDevice(mTcpClient);
    outStream.setVersion(QDataStream::Qt_5_11);

    // serialize
    outStream << imgArray.size();
    outStream << imgArray;

    // send
    mTcpClient.write(data);
    mTcpClient->waitForBytesWritten();
    mTcpClient->flush();

    qDebug() << "Sent" << data.size() << "bytes";
    }


    .. and for reading.



    void VCameraClient::readData()
    {
    qDebug() << "Received" << mTcpSocket->bytesAvailable() << "bytes";


    if (size == 0) {
    // read the image size
    // toBeRead is a class private member
    inputStream >> toBeRead;
    }

    // read partial data
    // byteArr is a class private member
    if (mTcpSocket->bytesAvailable() < toBeRead) {
    byteArr.append(mTcpSocket->read(mTcpSocket->bytesAvailable()));

    // update the size to be read
    toBeRead -= byteArr.size();
    }

    // read completed
    if (toBeRead <= 0) {
    mframe = QImage((const uchar*) byteArr.data(), 640, 480, QImage::FormatRGB888);
    emit newFrameRead(mFrame);
    }
    }


    Could anyone help me, please? How to use QDataStream transactions properly?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm working on a simple Qt application for video streaming over TCP. There is a device (server side) that start listening for incoming connections. When a new connection is created, the server start to send images to the client. On the other side, the client receive and display the images.
      I've read a lot about IPC on Qt, datastream and transactions, but I'm yet struggling with reading the incoming data. I know that TCP divide a long block in several packets, that's the point I'm in trouble.



      Here is my code for sending ..



      void CameraServer::sendData(const QImage &img)
      {
      if (mTcpClient->state() == QTcpSocket::ConnectedState) {

      QByteArray imgArray = QByteArray::fromRawData((const char*) img.bits(), img.byteCount());

      // init
      QByteArray data;
      QDataStream outStream(&data, QIODevice::WriteOnly);
      outStream.setDevice(mTcpClient);
      outStream.setVersion(QDataStream::Qt_5_11);

      // serialize
      outStream << imgArray.size();
      outStream << imgArray;

      // send
      mTcpClient.write(data);
      mTcpClient->waitForBytesWritten();
      mTcpClient->flush();

      qDebug() << "Sent" << data.size() << "bytes";
      }


      .. and for reading.



      void VCameraClient::readData()
      {
      qDebug() << "Received" << mTcpSocket->bytesAvailable() << "bytes";


      if (size == 0) {
      // read the image size
      // toBeRead is a class private member
      inputStream >> toBeRead;
      }

      // read partial data
      // byteArr is a class private member
      if (mTcpSocket->bytesAvailable() < toBeRead) {
      byteArr.append(mTcpSocket->read(mTcpSocket->bytesAvailable()));

      // update the size to be read
      toBeRead -= byteArr.size();
      }

      // read completed
      if (toBeRead <= 0) {
      mframe = QImage((const uchar*) byteArr.data(), 640, 480, QImage::FormatRGB888);
      emit newFrameRead(mFrame);
      }
      }


      Could anyone help me, please? How to use QDataStream transactions properly?










      share|improve this question















      I'm working on a simple Qt application for video streaming over TCP. There is a device (server side) that start listening for incoming connections. When a new connection is created, the server start to send images to the client. On the other side, the client receive and display the images.
      I've read a lot about IPC on Qt, datastream and transactions, but I'm yet struggling with reading the incoming data. I know that TCP divide a long block in several packets, that's the point I'm in trouble.



      Here is my code for sending ..



      void CameraServer::sendData(const QImage &img)
      {
      if (mTcpClient->state() == QTcpSocket::ConnectedState) {

      QByteArray imgArray = QByteArray::fromRawData((const char*) img.bits(), img.byteCount());

      // init
      QByteArray data;
      QDataStream outStream(&data, QIODevice::WriteOnly);
      outStream.setDevice(mTcpClient);
      outStream.setVersion(QDataStream::Qt_5_11);

      // serialize
      outStream << imgArray.size();
      outStream << imgArray;

      // send
      mTcpClient.write(data);
      mTcpClient->waitForBytesWritten();
      mTcpClient->flush();

      qDebug() << "Sent" << data.size() << "bytes";
      }


      .. and for reading.



      void VCameraClient::readData()
      {
      qDebug() << "Received" << mTcpSocket->bytesAvailable() << "bytes";


      if (size == 0) {
      // read the image size
      // toBeRead is a class private member
      inputStream >> toBeRead;
      }

      // read partial data
      // byteArr is a class private member
      if (mTcpSocket->bytesAvailable() < toBeRead) {
      byteArr.append(mTcpSocket->read(mTcpSocket->bytesAvailable()));

      // update the size to be read
      toBeRead -= byteArr.size();
      }

      // read completed
      if (toBeRead <= 0) {
      mframe = QImage((const uchar*) byteArr.data(), 640, 480, QImage::FormatRGB888);
      emit newFrameRead(mFrame);
      }
      }


      Could anyone help me, please? How to use QDataStream transactions properly?







      ipc qimage qtcpsocket qtcpserver






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 16:18

























      asked Nov 19 at 15:58









      gont_

      35




      35





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378393%2fsend-receive-qimage-with-qtcpsocket%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378393%2fsend-receive-qimage-with-qtcpsocket%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen