Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTcpSocket does not send data sometimes

Tags:

qt

I have two QT apps. One app can be considered to hold a big data and it sends about 10 KB of data chunk every second to second application.

Earlier I tried using QUdpSocket to transmit the data but due to MTU limitation of about 2-5K and need to divide and reunite the data myself, I switched to QTcpSocket.

Sometimes data is sent correctly with QTcpSocket (especially if I write data very frequently ~every 100 ms) but sometimes data is not sent at all. Not even after 5 sec. And sometimes several data chunks 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"; //this NEVER occurs in MY case.
    m_socket->flush();
}

On receiver side, I use readyRead signal to know when data has arrived.

How can I ensure that data is sent immediately? Are there any better alternatives for what I am trying to do?

Edit:: When I am writing after long gaps of 1 second, I receive "QAbstractSocket::SocketTimeoutError" on receiver side everytime sender sends data. This error is not received if sender writes data frequently.
Is it OKAY to use QTcpSocket to stream data the way I am doing????

Edit 2: On receiver side, when readyRead signal is emitted, I was again checking while(m_socket->waitForReadyRead(500)) and I was getting "QAbstractSocket::SocketTimeoutError" due to this. Also, this check was preventing delivering of single chunks.
After going through docs more, it seems readyRead will be continuously emitted when new data is available, so no need for waitForReadyRead.
I am receiving all data sent but still data does not come immediately. Sometimes two-three chunks are merged. This may be because of delay on receiver side in reading data etc.

like image 323
vivek.m Avatar asked Mar 15 '12 06:03

vivek.m


1 Answers

On receiver side, when readyRead signal is emitted, I was again checking while(m_socket->waitForReadyRead(500)) and I was getting "QAbstractSocket::SocketTimeoutError" due to this. Also, this check was preventing delivering of single chunks.

After going through docs more, it seems readyRead will be continuously emitted when new data is available, so there is no need for waitForReadyRead. It had solved my issue.

like image 84
vivek.m Avatar answered Nov 15 '22 07:11

vivek.m