I'm having issues with receiving a transfer.
QTcpSocket->readAll() not reads enough bytes when I'm sending to it. When I send like a 15k bytes, it reads only some part of it and then does nothing. What I'm doing wrong?
QByteArray array;
array = socket->readAll(); //just reads some part, not fully.
why?
Sorry for my English.
Regards.
Most probably the socket didn't receive all data yet when you call
readAll()
. This is because of TCP communication happens in small packets (each having around 1KB of data, depending on a lot of things). These packets make up a stream in which the other end of the communication line writes bytes into. You have to assemble them on the receiving side. How you assemble them has to be defined in a protocol.To solve this issue, you have to wait for all expected data before assembling it. Sometimes it is not known how much data is expected unless you read it (depending on the protocol).
Let's say you want to implement a protocol which says "everything until a line break is something we call a message". Now you want to receive such a message. This is done by successively reading and appending to a target buffer (like your
QByteArray
) until there comes a line break. However, there is another thing: When you expect a second message, it can be immediately after the first one in the TCP stream, so you just read not only the end of the first message, but also the beginning of the second. Just keep this in mind.When not dealing with signal slot connection, you can write a synchronous receiver for such newline-separated messages like this:
When handling
QTcpSocket::readyRead()
, you can do something similar.When you want to read everything sent via one TCP connection (until it gets closed by the peer), you can wait for this event either in a blocking way or process the data in a slot connected to the proper signal:
QTcpSocket::disconnected
.Blocking:
Non-blocking (handling signals using slots):
Alternative non-blocking solution (essentially the same):