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
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:
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));
printf("Err: writeSaveJSON failed\n");
QDataStream p_DataStream(&p_ByteArray, QIODevice::ReadOnly);
p_ByteArray = i_Socket->read(sizeof(int));
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);
SENT_SIZE is set to 4096, but that should not matter, I hope.
@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?
IamSumit last edited by
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().
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