Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

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;
            m_webSocket->setProxy(QNetworkProxy::NoProxy);
             connect(m_webSocket, &QWebSocket::textMessageRecieved,this, &CloudCommunicationManager::onTextMessageReceived);
            connect(m_Timer,SIGNAL(timeout()),this,SLOT(fnfCheckServerConnection()));
            connect(ReadJSON::GetInstance(),SIGNAL(sendRequestToCloud(QString)),this,SLOT(RequestFromBl(QString)));
         
            if(NULL != m_webSocket)
            {
                m_webSocket->open(QUrl(m_url));
                m_webSocket->ping();
            }
            m_Timer->start(1000*10);
    
        }
    void CloudCommunicationManager::onTextMessageRecieved(QString message)   
    {    qDebug() << "Message received From Cloud :" <<endl<< message;
            QByteArray str;
            str.append(message);
    
             RequestResponseHandler::GetInstance()->sendrequest(str);
              qDebug()<<"Cloud Msg given to TcpSocket to send to BL"<<endl;
    }
    
    void CloudCommunicationManager::RequestFromBl(QString msg)  
    {
    m_webSocket->sendTextMessage(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;
            }else
            {
                 status = true;
                tcpSocket->waitForBytesWritten(3000);
            }
            qDebug() << "Total Byte written " << qTotByteWritten ;
            tcpSocket->flush();
        }
      return status;
    }
    
    void CommunicationManager::ProcessResponse(void)
    {
       qDebug() << "Processing Response Message from BL " << endl;
        //read all data from socket
        QByteArray response = tcpSocket->readAll();
        tcpSocket->flush();
        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

    http://doc.qt.io/qt-5/qiodevice.html



  • @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