Looped QTcpSocket writes failing



  • I have a timer that triggers every 10 seconds. When the timer is triggered, the application pops into a for loop which sends out a certain amount of packets depending on what a value in a specific variable holds (could be from 0 to 20). I have to send these packets out separately no matter what. This Qt application actually communicates to a VB6 application through a QTcpSocket, as stated before. My system is Windows 7 using the QtCreator building for mscv2008.

    Here's some example code to show what I am talking about.

    @
    /* loop through /
    for (i=0;i<20;i++){
    /
    check server connection after every loop */
    if (serverSocket->state() != QTcpSocket::ConnectedState)
    return;

        // send data
        serverSocket->write(QVariant(i).toString().toLatin1());
        qDebug() << serverSocket->flush();
    }
    

    @

    The problem is, on the VB6 side, only 2 packets of the data get received.... 1 and 2...the remaining get lost or simply are not sent? I read somewhere online that if you are in a loop and want to send out data through a QTcpSocket multiple times, then you have to flush the socket after the send to make sure the data gets sent out due to you not leaving the event loop.

    Is there another solution?

    p.s the output of the qDebug() statement does indeed show that the buffer was flushed 20 times. But the data is lost....



  • The data will not be sent until your code returns to the Qt event loop or explicitly calls waitForBytesWritten(): calls to write() are not blocking and only queue data for later transmission. The flush() call does not immediately send everything queued in Qt: it only queues some amount with the OS (see the docs).

    The data may, or may not, arrive at the receiver in the same number of chunks as you queued it in. There is no connection between the number of calls you make to write() and the number of TCP packets sent. TCP guarantees that all bytes sent are received in the correct order.


Log in to reply
 

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