Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why QTcpSocket read(len) has prefix ?



  • 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......" )


  • Lifetime Qt Champion

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


  • Moderators

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



  • 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


Log in to reply