Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can write multi packet over QTcpSocket??

How can write multi packet over QTcpSocket??

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 2.4k 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.
  • I Offline
    I Offline
    isan
    wrote on last edited by isan
    #1

    I want to send multi-packet and read receive data and appended to QTextEdit at same time, but it's Interchangeably and mixed when read receive data and appended to QtextEdit,how can send multi-packet without problem??

    QByteArray QTcp::Data{
    SendTCPData(send->inv());
        QByteArray rcv=readData();
        QString in(rcv.toHex());
        QTextCursor cursor = ui->textEdit_2->textCursor();
        QTextBlockFormat textBlockFormat = cursor.blockFormat();
        textBlockFormat.setAlignment(Qt::AlignLeft);
        cursor.mergeBlockFormat(textBlockFormat);
        ui->textEdit_2->setTextCursor(cursor);
        ui->textEdit_2->setTextColor(Qt::black);
     readData().clear();
    
        usleep(3000000);
    
            SendTCPData(send->SendWrt(epc));
            QByteArray rcv1=readData();
            QString wr(rcv1.toHex());
            textBlockFormat.setAlignment(Qt::AlignRight);
            ui->textEdit_2->setTextColor(Qt::darkGray);
                ui->textEdit_2->append(wr);
                readData().clear();
            }
        
      int QTcp::SendTCPData(QByteArray Data)
    {
        bool connected = (_socket->state() == QTcpSocket::ConnectedState);
        if(connected){
            _socket->write(Data, Data.size());
            qDebug()<<"Data send"<<Data.toHex();
            return _socket->waitForBytesWritten(5000);
        }
        else{
            qDebug()<<"Not Connected";
            return false;
        }
    
    QByteArray QTcp::readData()
    {
        QByteArray data= _socket->readAll();
        qDebug()<<"readData"<<data.toHex();
        return data;
    }
    
    }
    
    
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Your setup is a bit strange. Why don't you use the asynchronous nature of QTcpSocket to handle your data transmission ? You should use the QTcpSocket::readyRead to know when there's new data to read and then update your text edit with it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        isan
        wrote on last edited by
        #3

        yes I used connect(_server, SIGNAL(newConnection()), this, SLOT(NewConnection()));
        to printout when new connection receive and in NewConnection() function I set connect(_socket, SIGNAL(readyRead()),this, SLOT(readData()));
        but when read data,for example the answer of first sendTcp print out for second sendTcp data and Vice versa

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Except that from your sample code you have a pseudo synchronous system:

          • You send data to your TCP socket
          • You read from it,
          • Do some update
          • Sleep
          • Start again.

          That's not how it works. Since you are doing a two-way communication, you should send that data. And once you received the complete answer send the second wave of data.

          In ready read, buffer the data you received until you received everything and then parse it update your widgets.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • I Offline
            I Offline
            isan
            wrote on last edited by
            #5

            @SGaist said:

            In ready read, buffer the data you received until you received everything and then parse it update your widgets

            you mean ReadyRead do this ???
            or I should do this??

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You should do this. You should have a slot that reads the data from the socket and append it to a buffer so that you can parse the answer once it's complete.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • I Offline
                I Offline
                isan
                wrote on last edited by
                #7

                ok tnx I try this

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  isan
                  wrote on last edited by isan
                  #8

                  I do this , if i send one package each time write data , read data and printout data is true but when appended data to QtextEdit first appended last read data then if send again appended new read data
                  I use

                  ui->textEdit->textCursor().insertText(pwr);
                  ui->textEdit->textCursor().clearSelection();
                  ui->textEdit->textCursor().deletePreviousChar();
                  
                  

                  but still first show last appended

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Why deletePreviousChar ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      isan
                      wrote on last edited by
                      #10
                      This post is deleted!
                      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