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. Why QTcpSocket read(len) has prefix ?
Forum Updated to NodeBB v4.3 + New Features

Why QTcpSocket read(len) has prefix ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 325 Views 1 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by sonichy
    #1
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMessage()));
    
    void MainWindow::readMessage()
    { 
        qint64 len = tcpSocket->bytesAvailable();    
        qDebug() << "receive(" << len << ")";
        QByteArray BA = tcpSocket->read(len);
        qDebug() << "ByteArray(" << BA << ")";
    }
    

    old.htm (file name)
    ByteArray( "\x00\x07old.htm" )

    file content
    ByteArray( "\x00\x00\x00\x00\x00\x00\b\xA2<html>\n......" )

    https://github.com/sonichy

    jsulmJ 1 Reply Last reply
    0
    • sonichyS sonichy
      connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMessage()));
      
      void MainWindow::readMessage()
      { 
          qint64 len = tcpSocket->bytesAvailable();    
          qDebug() << "receive(" << len << ")";
          QByteArray BA = tcpSocket->read(len);
          qDebug() << "ByteArray(" << BA << ")";
      }
      

      old.htm (file name)
      ByteArray( "\x00\x07old.htm" )

      file content
      ByteArray( "\x00\x00\x00\x00\x00\x00\b\xA2<html>\n......" )

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

      @sonichy Please explain what you mean. What prefix?
      Also, don't forget that the data you're sending can arrive in several packets. That means: tcpSocket->read(len) does not necessarily return the whole package you sent. You need to accumulate data in a QByteArray until you notice that you got whole package.

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

      sierdzioS 1 Reply Last reply
      2
      • jsulmJ jsulm

        @sonichy Please explain what you mean. What prefix?
        Also, don't forget that the data you're sending can arrive in several packets. That means: tcpSocket->read(len) does not necessarily return the whole package you sent. You need to accumulate data in a QByteArray until you notice that you got whole package.

        sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        @jsulm said in Why QTcpSocket read(len) has prefix ?:

        @sonichy Please explain what you mean. What prefix?

        He (claims he) is sending old.htm but receiving \x00\x07old.htm.

        (Z(:^

        1 Reply Last reply
        2
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          How are you writing those? \x00\x07 is the size of the subsequent string as a 16bit integer. would make sense to me as a header written by a structured serialiser

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          4

          • Login

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