Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] QTcpSocket would not receiving all data



  • All I am working on with some of the QT Networking tools to send data between two laptops. I am having an issue where QTcpSocket does not seem to receive all the data sent. I have used wireshark and check bytes written (from server) returned from the socket-> write function. It is strange because one execution I will get all 4999 packets (all packets) and if I execute it again it will receive only 2920 packets. I have tried to implement all the tips from other threads but I am clearly missing something. Here is my code and any help is appreciated

    Server Side Writing the data

    @
    void TcpServer::writeData()
    {
    QByteArray test;

    for(int ctr = 1; ctr < 5000; ctr++)
    {
        test.append("A");
    }
    
    qDebug() << "Inside Write";
    
    QTcpSocket *socket = new QTcpSocket(this);
    socket->connectToHost(QHostAddress("156.80.124.103"), 1234);
    qint64 bytes = socket->write(test);
    socket->waitForBytesWritten();
    socket->close();
    
    qDebug() << "Bytes written = " << bytes;
    

    }
    @

    One the client side I am reading the data back in.

    @
    void myServer::newConnection()
    {
    qDebug() << "Detected Connection";
    QByteArray testing;
    QTcpSocket *socket = server->nextPendingConnection();
    qDebug() << "Wait for connect = " << socket->waitForConnected();

    if(socket->waitForReadyRead(3000))
    {
        while(socket->bytesAvailable() > 0)
        {
            testing.append(socket->readAll());
            socket->flush();
        }
    }
    else
    {
        qDebug() << "ReadyReady Timed out";
    }
    qDebug() << "Final Testing is size = " << testing.size();
    socket->deleteLater();
    

    }
    @



  • HI,

    try with

    @void myServer::newConnection()
    {
    qDebug() << "Detected Connection";
    QByteArray testing;
    QTcpSocket *socket = server->nextPendingConnection();
    qDebug() << "Wait for connect = " << socket->waitForConnected();

    while (socket->waitForReadyRead(3000))
    {
        while(socket->bytesAvailable() > 0)
        {
            testing.append(socket->readAll());
            socket->flush();
        }
    }
    qDebug() << "ReadyReady Timed out";
    qDebug() << "Final Testing is size = " << testing.size();
    socket->deleteLater();
    

    }@

    this why once you read all data from socket more data can arrive



  • Thank you for the response. The first time I get 4999 and the second time with the else removed I still get 2920. Very strange. the output of the debug for two executions reads as follows.

    Server Started
    Detected Connection
    Wait for connect = true
    Final Testing is size = 4999
    Detected Connection
    Wait for connect = true
    Final Testing is size = 2920

    I just don't get why it would run once but have issues the second time. No matter how many times I run it after the initial run its 2920 packets. Strange



  • Thank you that worked. The while loop on the waitForReady. Thank you so much!


Log in to reply