Qt6 topics merged with General

loss of packet in QTcpSocket communication

  • Hi,
    There is one QTcpSocket (process 1)which is communicating with the other process.(process 2)
    For some reason I have created a web Socket (in a separate thread) in process1.
    And the response what I get from cloud is again delegated to QTcpSocket to send it to process 2.
    TCP communication is also in separate thread.

    My Client is saying that there is a loss of packet.
    what could be the reason?
    should I create another QTcpSocket for web related messages.
    With Thanks

  • Qt Champions 2017

    difficult find reason based on this. Any sample code ? It is possible that you must reading some data twice using readAll function and not sending properly. Some times, readAll is part of debug message which you would not have noticed.

  • Lifetime Qt Champion

    @Phadnis said in loss of packet in QTcpSocket communication:

    TCP communication is also in separate thread.

    Is there a reason for that?
    Qt network communication is asynchronous.

  • @dheerendra
    Thank You Sir.
    Below is the code
    //your code here

    void CloudCommunicationManager::onThreadStart()
    {    m_webSocket = new QWebSocket;
            m_Timer = new QTimer;
             connect(m_webSocket, &QWebSocket::textMessageRecieved,this, &CloudCommunicationManager::onTextMessageReceived);
            if(NULL != m_webSocket)
    void CloudCommunicationManager::onTextMessageRecieved(QString message)   
    {    qDebug() << "Message received From Cloud :" <<endl<< message;
            QByteArray str;
              qDebug()<<"Cloud Msg given to TcpSocket to send to BL"<<endl;
    void CloudCommunicationManager::RequestFromBl(QString msg)  
    // start of TCP communication
    bool CommunicationManager::ProcessRequest(QByteArray requestDataArray)
      qint64 qTotByteWritten = -1;
     qDebug()<< "Data to be sent " << requestDataArray ;
     qDebug()<<"size of sent data" << requestDataArray.size();
       qTotByteWritten = tcpSocket->write(requestDataArray);
            if(qTotByteWritten == -1)
                status = false;
                qDebug()<<"Failed to write on Server" << endl;
                 status = true;
            qDebug() << "Total Byte written " << qTotByteWritten ;
      return status;
    void CommunicationManager::ProcessResponse(void)
       qDebug() << "Processing Response Message from BL " << endl;
        //read all data from socket
        QByteArray response = tcpSocket->readAll();
        emit ResponseFromServer(response);

  • @jsulm
    Yeah I went thro doc after seeing your reply.
    Thank you.

  • Qt Champions 2017

    Your problem is resolved or still you have problem ?

  • @dheerendra
    Client had hit the issue once.For us it is working fine.
    It is being tested.

  • @Phadnis
    I might be mistaken here, but...

    In your code, how do you know that tcpSocket->readAll() has read all that the other side has sent? And how do you know that tcpSocket->write(requestDataArray); has written all the bytes to send? I see no loops....

  • @dheerendra
    In the process 2 buffer size was 2048, but the message size was 2.4kB. So message would truncate.
    In the Process 2 side buffer size was reduced to 1024. And in Process 1 side Cloud message was split into multiple packets of size 1024.
    Thank You Sir.

  • @JonB
    bool QIODevice::isSequential() const ........See the Documentation of QIODevice Class this may clear your doubt


  • @Phadnis

    • I do not see any relevance to QIODevice::isSequential() in your case.

    • I do not see how adjusting buffer sizes at server/client side has any relevance to supporting robust communication between client/server. Regardless of any buffer sizes, TCP/IP/sockets communicate with a streaming protocol, wherein any sized packet/buffer can be received up to the size sent by the other end, but it can be any amount less than that. Which means that reader must always loop to be sure it has received all desired sender's data. Which you do not have.

    So up to you if you seem to be happy with your code....

Log in to reply