Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Communication via QTcpSocket for data > 8192 bytes



  • 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


  • Lifetime Qt Champion

    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



  • 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?



  • 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().



  • 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


Log in to reply