Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Send and Receive Double Array (double * ) over QTcpSocket
Forum Update on Monday, May 27th 2025

Send and Receive Double Array (double * ) over QTcpSocket

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ayse
    wrote on 6 Aug 2018, 12:02 last edited by Ayse 8 Jun 2018, 12:29
    #1

    In QT C++ I can send packet over QTcpSocket by using QDataStream for double, int , QString.. data types. But I want to send array of doubles by using QDataStream. My double array function in shown in this code block.

    double * array = generatePacketDoubleArray(1,10,0.001);
    

    Generate Double Array Function :

    double * Producer::generatePacketDoubleArray(int packetNum,int maxlen,double stepDouble)
    {
        double *array = new double[maxlen];
    
        double increase = 0;
    
        for(int i=0;i<maxlen;++i)
        {
            increase += stepDouble;
            array[i] = stepDouble*maxlen*packetNum+increase;
        }
        return array;
    }
    

    I want to send my double array packet same as this code blocks by using QDataStream in Server Side and Receive double * from QDataStream in Client Side . But I could not find a solution for cast double * to QByteArray . I want to learn this cast operation.

    Regards..

            QByteArray block;
            QDataStream out(&block, QIODevice::WriteOnly);
            out.setVersion(QDataStream::Qt_4_0);
    
            out << static_cast<qint32>(packet.packetNum)
                << static_cast<qint32>(packet.data)
                << static_cast<qint32>(packet.packetSize);
    
    A 1 Reply Last reply 6 Aug 2018, 13:17
    0
    • A Ayse
      6 Aug 2018, 12:02

      In QT C++ I can send packet over QTcpSocket by using QDataStream for double, int , QString.. data types. But I want to send array of doubles by using QDataStream. My double array function in shown in this code block.

      double * array = generatePacketDoubleArray(1,10,0.001);
      

      Generate Double Array Function :

      double * Producer::generatePacketDoubleArray(int packetNum,int maxlen,double stepDouble)
      {
          double *array = new double[maxlen];
      
          double increase = 0;
      
          for(int i=0;i<maxlen;++i)
          {
              increase += stepDouble;
              array[i] = stepDouble*maxlen*packetNum+increase;
          }
          return array;
      }
      

      I want to send my double array packet same as this code blocks by using QDataStream in Server Side and Receive double * from QDataStream in Client Side . But I could not find a solution for cast double * to QByteArray . I want to learn this cast operation.

      Regards..

              QByteArray block;
              QDataStream out(&block, QIODevice::WriteOnly);
              out.setVersion(QDataStream::Qt_4_0);
      
              out << static_cast<qint32>(packet.packetNum)
                  << static_cast<qint32>(packet.data)
                  << static_cast<qint32>(packet.packetSize);
      
      A Offline
      A Offline
      Ayse
      wrote on 6 Aug 2018, 13:17 last edited by Ayse 8 Jun 2018, 13:44
      #2

      @Ayse

      I found solution ;

      In Server Side :

              doubleArray = generatePacketDoubleArray(counter,maxlen,0.001);
              Packet packet(counter,doubleArray);
      
              QByteArray block;
              QDataStream out(&block, QIODevice::WriteOnly);
              out.setVersion(QDataStream::Qt_4_0);
      
              out << static_cast<qint32>(packet.packetNum)
                  << QByteArray::fromRawData(reinterpret_cast<const char*>(packet.doubleArrayData),maxlen*sizeof(double));
      
              qDebug() << "Packet Num : " << counter << " Bytes Written -> " << tcpServerConnection->write(block);
      
              tcpServerConnection->waitForBytesWritten();
      
      

      In Client Side :

              qint32 packetNum ;
              QByteArray byteArray;
      
              in >>packetNum >> byteArray;
      
              qDebug() << "Packet Num : " <<packetNum;
      
              double doubleArray[maxlen];
              memcpy(doubleArray, byteArray.data(),byteArray.count());
      
              for(int i = 0;i<maxlen;i++)
                  printf("%f   ",doubleArray[i]);
              printf("\n");
      
      
      1 Reply Last reply
      0
      • B Offline
        B Offline
        Buckwheat
        wrote on 6 Aug 2018, 13:41 last edited by
        #3

        @Ayse, Great recovery! I was looking at your first post and saying... how do I read the array when I don't know how long it is! :D

        Dave Fileccia

        1 Reply Last reply
        0

        1/3

        6 Aug 2018, 12:02

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved