I have two QT applications. We can assume that one application contains big data and sends about 10 KB of data every time per second for the second application.
I used to try to use QUdpSocket to transfer data, but due to the MTU limitation of about 2-5K and the need to divide and reconnect data, I switched to QTcpSocket.
Sometimes data is sent correctly with QTcpSocket (especially if I write data very often ~ every 100 ms), but sometimes data is not sent at all. Even after 5 seconds. And sometimes several data blocks are internally buffered for a long period (several seconds), and then sent together.
m_socket = new QTcpSocket(this); m_socket->connectToHost(QHostAddress::LocalHost, 45454); sendDataEverySec() { QByteArray datagram(10000, 'a'); qint64 len = m_socket->write(datagram); if(len != datagram.size()) qDebug() << "Error";
On the receiver side, I use the readyRead
signal to know when the data arrived.
How can I guarantee the immediate sending of data? Are there any better alternatives to what I'm trying to do?
Edit :: When I write after long spaces in 1 second, I get "QAbstractSocket::SocketTimeoutError"
on the receiver side every time the sender sends data. This error is not accepted if the sender often writes data.
Can I use QTcpSocket to stream data the way I do?
Edit 2: on the receiver side, when the readyRead
signal is issued, I checked while(m_socket->waitForReadyRead(500))
again, and because of this I got "QAbstractSocket::SocketTimeoutError"
. In addition, this check prevented the delivery of individual pieces.
After looking at additional documents, it seems that readyRead
will be continuously emitted when new data is available, so there is no need for waitForReadyRead
.
I get all the data sent, but still the data does not arrive immediately. Sometimes two or three pieces come together. This may be due to a delay on the receiver side when reading data, etc.
source share