Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. loss of packet in QTcpSocket communication
Forum Updated to NodeBB v4.3 + New Features

loss of packet in QTcpSocket communication

Scheduled Pinned Locked Moved Solved Mobile and Embedded
11 Posts 4 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phadnis
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      P 1 Reply Last reply
      2
      • P Phadnis

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply
        1
        • dheerendraD dheerendra

          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.

          P Offline
          P Offline
          Phadnis
          wrote on last edited by
          #4

          @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);
          
          }
          1 Reply Last reply
          0
          • jsulmJ jsulm

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

            P Offline
            P Offline
            Phadnis
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              Your problem is resolved or still you have problem ?

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              P 2 Replies Last reply
              0
              • dheerendraD dheerendra

                Your problem is resolved or still you have problem ?

                P Offline
                P Offline
                Phadnis
                wrote on last edited by
                #7

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

                JonBJ 1 Reply Last reply
                0
                • P Phadnis

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

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

                  P 1 Reply Last reply
                  1
                  • dheerendraD dheerendra

                    Your problem is resolved or still you have problem ?

                    P Offline
                    P Offline
                    Phadnis
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      P Offline
                      P Offline
                      Phadnis
                      wrote on last edited by
                      #10

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

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

                      JonBJ 1 Reply Last reply
                      0
                      • P Phadnis

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

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved