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:
    QByteArray p_ByteArray;
    QDataStream p_DataSteam(&p_ByteArray, QIODevice::WriteOnly);
    p_DataSteam << (int) -1;
    p_DataSteam << (int) p_ByteArray.size();

    int p_Sent = 0;
    p_Sent += i_Socket->write(p_ByteArray.mid(p_Sent,SENT_SIZE));

    return EXIT_SUCCESS;
    printf("Err: writeSaveJSON failed\n");
    return EXIT_FAILURE;

    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
    printf("Err: readSaveJSON failed %d %d\n",p_ByteArray.size(),p_ByteSize);
    return QJsonDocument::fromJson(p_ByteArray).object();

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

    Any ideas?

    kind regards,

  • Lifetime Qt Champion


    @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.
    " 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

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.