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. Communication via QTcpSocket for data > 8192 bytes
Qt 6.11 is out! See what's new in the release blog

Communication via QTcpSocket for data > 8192 bytes

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.2k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hey there,
    I want to exchange a QJsonObject between Server and Client. My code works for not to large data, but when the data is above a size of 8192 Bytes, it doesn't work anymore, although I am sure I take care of multiple packages.
    Maybe someone has an idea.
    Code as follows:
    @
    //write
    QByteArray p_ByteArray;
    QDataStream p_DataSteam(&p_ByteArray, QIODevice::WriteOnly);
    p_DataSteam << (int) -1;
    p_ByteArray.append(QJsonDocument(i_JsonObject).toJson());
    p_DataSteam.device()->seek(0);
    p_DataSteam << (int) p_ByteArray.size();

    int p_Sent = 0;
    while(p_Sent<p_ByteArray.size())
    {
    p_Sent += i_Socket->write(p_ByteArray.mid(p_Sent,SENT_SIZE));
    i_Socket->waitForBytesWritten(WAIT_TIME);
    }

    if(p_Sent==p_ByteArray.size())
    return EXIT_SUCCESS;
    else
    {
    printf("Err: writeSaveJSON failed\n");
    return EXIT_FAILURE;
    }
    @

    @
    //read
    QByteArray p_ByteArray;
    QDataStream p_DataStream(&p_ByteArray, QIODevice::ReadOnly);
    p_ByteArray = i_Socket->read(sizeof(int));

    int p_ByteSize;
    p_DataStream >> p_ByteSize;

    while(p_ByteArray.size()<p_ByteSize) // endless loop, since no further data received
    {
    int p_ToRead = p_ByteSize - (int)p_ByteArray.size();
    QByteArray p_ByteArray2 = i_Socket->read(p_ToRead); // this one is empty after first call
    p_ByteArray.append(p_ByteArray2);
    //p_ByteArray.append(i_Socket->read(p_ByteSize-p_ByteArray.size()));
    }
    if(p_ByteArray.size()!=p_ByteSize)
    {
    printf("Err: readSaveJSON failed %d %d\n",p_ByteArray.size(),p_ByteSize);
    }
    p_ByteArray=p_ByteArray.mid(sizeof(int),p_ByteArray.size()-sizeof(int));
    return QJsonDocument::fromJson(p_ByteArray).object();
    @

    SENT_SIZE is set to 4096, but that should not matter, I hope.

    Any ideas?

    kind regards,
    curator

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      @p_DataSteam << (int) p_ByteArray.size(); << wrong size@

      You are giving the size of the complete array, you should only give the payload size

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Hey, thanks for replying.

        If I get your answer correctly, you say that I send the size of the commited data, where the commited data is extended by the size of the original data. that is correct, but as long as I know that and take care of that in the receiving function, that should not matter?

        Any further ideas or corrections of this post?

        1 Reply Last reply
        0
        • IamSumitI Offline
          IamSumitI Offline
          IamSumit
          wrote on last edited by
          #4

          Hi.
          From the above you are writing 4096 bytes and reading only 4 bytes.
          here
          " p_ByteArray = i_Socket->read(sizeof(int));"
          update it by

          • p_ByteArray = i_Socket->read(4096);*

          if does not work then use readAll().

          Be Cute

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Hi,
            The line you mention only ready the size of the array, the main read is in line 12.

            However, I switched temporary to readAll and it stops after 8192 bytes again

            1 Reply Last reply
            0

            • Login

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