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 to get formatted message from TcpSocket

How to get formatted message from TcpSocket

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 711 Views 2 Watching
  • 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.
  • S Offline
    S Offline
    SilentWolf
    wrote on last edited by SilentWolf
    #1

    I have message which send to TcpSocket with format: 0x02 + msg + 0x03
    start: 0x02
    end: 0x03
    How I can got msg from this.

    void TcpAcServer::newConnection() {
    QTcpSocket *newClient = server->nextPendingConnection();
    clients << newClient;
    
    connect(newClient, SIGNAL(readyRead()), this, SLOT(receivedData()));
    connect(newClient, SIGNAL(disconnected()), this, SLOT(disconnected()));
    }
    
    
    void TcpAcServer::receivedData() {
    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
    if (socket == 0) { // Neu khong xac dinh duoc nguon phat, chung ta se dung xu ly
    return;
    }
    
    // TODO: how to extract msg ???
    
    JonBJ 1 Reply Last reply
    0
    • S SilentWolf

      I have message which send to TcpSocket with format: 0x02 + msg + 0x03
      start: 0x02
      end: 0x03
      How I can got msg from this.

      void TcpAcServer::newConnection() {
      QTcpSocket *newClient = server->nextPendingConnection();
      clients << newClient;
      
      connect(newClient, SIGNAL(readyRead()), this, SLOT(receivedData()));
      connect(newClient, SIGNAL(disconnected()), this, SLOT(disconnected()));
      }
      
      
      void TcpAcServer::receivedData() {
      QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
      if (socket == 0) { // Neu khong xac dinh duoc nguon phat, chung ta se dung xu ly
      return;
      }
      
      // TODO: how to extract msg ???
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @SilentWolf said in How to get formatted message from TcpSocket:

      I have message which send to TcpSocket with format: char(2) + msg + char(3)

      You can't until you define how you know the length of the msg segment, or how it is terminated. Or is the char(3) a particular pattern which is recognisable?

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @SilentWolf said in How to get formatted message from TcpSocket:

        I have message which send to TcpSocket with format: char(2) + msg + char(3)

        You can't until you define how you know the length of the msg segment, or how it is terminated. Or is the char(3) a particular pattern which is recognisable?

        S Offline
        S Offline
        SilentWolf
        wrote on last edited by
        #3

        @JonB said in How to get formatted message from TcpSocket:

        rticular patter

        Yes. char(3) is pattern which recognize end of msg segment.

        JonBJ 1 Reply Last reply
        0
        • S SilentWolf

          @JonB said in How to get formatted message from TcpSocket:

          rticular patter

          Yes. char(3) is pattern which recognize end of msg segment.

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

          @SilentWolf
          Then you need to keep accumulating bytes into a buffer (class variable) till you find find the char(3) pattern in the buffer. Then your message is to the left of that, skipping the first 2 characters. Remove that message with its leading & trailing bytes from the buffer and continue for next message.

          Of course if you service multiple connected clients you will need to recognise which one and have separate buffers for each client.

          QDataStream has Read Transactions, but I'm not sure your protocol lends itself to using that.

          S 1 Reply Last reply
          2
          • JonBJ JonB

            @SilentWolf
            Then you need to keep accumulating bytes into a buffer (class variable) till you find find the char(3) pattern in the buffer. Then your message is to the left of that, skipping the first 2 characters. Remove that message with its leading & trailing bytes from the buffer and continue for next message.

            Of course if you service multiple connected clients you will need to recognise which one and have separate buffers for each client.

            QDataStream has Read Transactions, but I'm not sure your protocol lends itself to using that.

            S Offline
            S Offline
            SilentWolf
            wrote on last edited by
            #5

            @JonB : I have trouble with how to code for:
            "accumulating bytes into a buffer (class variable) till you find find the char(3) pattern in the buffer"

            Using QDataStream is possible?

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

              Hi,

              Are both ends written with Qt ?

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

              S 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                Are both ends written with Qt ?

                S Offline
                S Offline
                SilentWolf
                wrote on last edited by
                #7

                @SGaist said in How to get formatted message from TcpSocket:

                nds written with Qt ?

                yes, I use QT5

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

                  Then yes go with QDataStream and transactions as suggest by @JonB.

                  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
                  • S Offline
                    S Offline
                    SilentWolf
                    wrote on last edited by
                    #9

                    Thank all,
                    I fixed my problem

                    QHash<QTcpSocket*, QByteArray*> clients; 
                    
                    void TcpAcServer::receivedData() {
                        QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
                        if (socket == 0) {
                            return;
                        }
                    
                        QByteArray *buffer = clients.value(socket);
                        while (socket->bytesAvailable() > 0) {
                            buffer->append(socket->readAll());
                            if (buffer->contains(0x03)){
                                QByteArray data = buffer->mid(0, buffer->size());
                                broadcastMessage(false, data);
                                buffer->remove(0, buffer->size());
                                break;
                            }
                        }
                    }
                    
                    1 Reply Last reply
                    1

                    • Login

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