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. QTcpSocket read packet
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket read packet

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 183 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.
  • R Offline
    R Offline
    Redman
    wrote on last edited by
    #1

    Hello,

    my application opens up a QTcpServer, waits for a connection and then retrieved a QTcpSocket from that attempted connection to retrieve incomming data. This all works, but now I am not sure on how to proceed from here.

    {
    .
    .
    .
    // Create tcp server for receiving  packets
       // Connect to slot if data is available to read
       m_tcpServer = QSharedPointer<QTcpServer>(new QTcpServer(this));
       connect(m_tcpServer.data(), &QTcpServer::newConnection, [&]() {
          // Retrieve client socket that tried to connect to tcp server
          m_tcpServerClient = QSharedPointer<QTcpSocket>(
              m_tcpServer.data()->nextPendingConnection());
    
          // Connect readyRead of client socket to receive packets
          connect(m_tcpServerClient.data(), &QTcpSocket::readyRead, this,
              &MycClass::recEchoDataReady);
          emit echoConnected();
       });
    
       m_tcpServer.data()->listen(ip, port);
    }
    
    void
    UlgSimu::recEchoDataReady() {
       QByteArray data = m_tcpServerClient.data()->readAll();
       transformDatagram(data);
    }
    
    

    Does data contain the whole packet? Does it only contain the payload?

    JonBJ 1 Reply Last reply
    0
    • R Redman

      Hello,

      my application opens up a QTcpServer, waits for a connection and then retrieved a QTcpSocket from that attempted connection to retrieve incomming data. This all works, but now I am not sure on how to proceed from here.

      {
      .
      .
      .
      // Create tcp server for receiving  packets
         // Connect to slot if data is available to read
         m_tcpServer = QSharedPointer<QTcpServer>(new QTcpServer(this));
         connect(m_tcpServer.data(), &QTcpServer::newConnection, [&]() {
            // Retrieve client socket that tried to connect to tcp server
            m_tcpServerClient = QSharedPointer<QTcpSocket>(
                m_tcpServer.data()->nextPendingConnection());
      
            // Connect readyRead of client socket to receive packets
            connect(m_tcpServerClient.data(), &QTcpSocket::readyRead, this,
                &MycClass::recEchoDataReady);
            emit echoConnected();
         });
      
         m_tcpServer.data()->listen(ip, port);
      }
      
      void
      UlgSimu::recEchoDataReady() {
         QByteArray data = m_tcpServerClient.data()->readAll();
         transformDatagram(data);
      }
      
      

      Does data contain the whole packet? Does it only contain the payload?

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

      @Redman
      You should not be talking about (or creating variables) for "packets", "payloads" or "datagrams" here. All you know is that QTcpSocket::readyRead signal will fire when 1 or more data bytes arrive at socket. That's it! It could be any number from 1 to the total number of data bytes (as returned by readAll() or bytesAvailable()) sent from the other side. And consequently it could be emitted any number of times with varying numbers of bytes. You should make no assumptions about how many will be available each time. If you need to "collect" a number of bytes to constitute some "message" your code expects to parse it is your job to buffer all the bytes received and work from that.

      1 Reply Last reply
      3
      • R Redman has marked this topic as solved on

      • Login

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