QTcpSocket.read returns only a single character in Release mode



  • The following Qt C++ source code successfully reads and processes 1024 size data chunks while running in Qt Debug mode. However, if running in release mode, only a single character appears to be in readBuffer after the message header has been successfully read. This may be a problem related to unicode data processing. Why would this code work when compiled in Debug mode, but not in Release mode?

    @
    //Code snippet
    memset(readBuffer, 0, sizeof(readBuffer));
    bytesReceived = mTCPSocket.read(readBuffer, ChunkSize);
    //bytesReceived shows correct amount, but readBuffer contains only a single character.

    //Complete Method (Works in Debug mode; however, read only gets the first character of each data chunk.
    void TCPClient::slotReceiveData()
    {
    Logger::getInstance()->write(LogLevel::Info, "TCPClient::slotReceiveData()");

    qint64 bytesReceived;
    static quint16 ChunkSize = TCPClient::READ_BUFFER_SIZE;  //1024
    char readBuffer[ChunkSize];
    
    if (mBytesExpected == 0)
    {
        MessageHeader messageHeader;
        int sizeofMessageHeader = sizeof(messageHeader);
    
        memset(readBuffer, 0, sizeof(readBuffer));
        bytesReceived = mTCPSocket.read(readBuffer, sizeofMessageHeader);
        memcpy(&messageHeader, readBuffer, sizeofMessageHeader);
    
        //QString signature = QString::number(messageHeader.Signature);
        //QString protocolVersion = QString::number(messageHeader.ProtocolVersion);
        QString dataLength = QString::number(messageHeader.DataLength);
    
        Logger::getInstance()->write(LogLevel::Info, "mBytesExpected = 0, dataLength = " + dataLength);
    
        mBuffer.clear();
        mBuffer.reserve(messageHeader.DataLength);
        mBytesExpected = messageHeader.DataLength;
    }
    
    memset(readBuffer, 0, sizeof(readBuffer));
    bytesReceived = mTCPSocket.read(readBuffer, ChunkSize);
    
    
    QString inData(readBuffer);
    Logger::getInstance()->write(LogLevel::Info, "mBytesExpected = " + QString::number(mBytesExpected));
    Logger::getInstance()->write(LogLevel::Info, "bytesReceived = " + QString::number(bytesReceived));
    Logger::getInstance()->write(
                     LogLevel::Info,
                     "readBuffer = " + inData);
    
    
    if(bytesReceived > 0)
    {
       mBuffer.append(readBuffer, bytesReceived);
       mBytesExpected -= bytesReceived;
    }
    else
    {
       mBytesExpected = 0;
       Logger::getInstance()->write(LogLevel::Error,
                                               "Error reading data from mTCPSocket.");
    }
    
    if (mBytesExpected == 0)
    {
        emit dataReceived(mBuffer.data());
    }
    

    }
    @

    //LOG OUTPUT IN RELEASE MODE

    INFO: 2014-03-04 16:48:10.873 (UTC): TCPClient::slotReceiveData()
    INFO: 2014-03-04 16:48:10.874 (UTC): mBytesExpected = 0, dataLength = 3104
    INFO: 2014-03-04 16:48:10.874 (UTC): mBytesExpected = 3104
    INFO: 2014-03-04 16:48:10.874 (UTC): bytesReceived = 996
    INFO: 2014-03-04 16:48:10.874 (UTC): readBuffer = <
    INFO: 2014-03-04 16:48:10.874 (UTC): TCPClient::slotReceiveData()
    INFO: 2014-03-04 16:48:10.874 (UTC): mBytesExpected = 2108
    INFO: 2014-03-04 16:48:10.874 (UTC): bytesReceived = 1024
    INFO: 2014-03-04 16:48:10.874 (UTC): readBuffer = 4
    INFO: 2014-03-04 16:48:10.874 (UTC): TCPClient::slotReceiveData()
    INFO: 2014-03-04 16:48:10.874 (UTC): mBytesExpected = 1084
    INFO: 2014-03-04 16:48:10.874 (UTC): bytesReceived = 1024
    INFO: 2014-03-04 16:48:10.874 (UTC): readBuffer = i
    INFO: 2014-03-04 16:48:10.874 (UTC): TCPClient::slotReceiveData()
    INFO: 2014-03-04 16:48:10.874 (UTC): mBytesExpected = 60
    INFO: 2014-03-04 16:48:10.875 (UTC): bytesReceived = 60
    INFO: 2014-03-04 16:48:10.875 (UTC): readBuffer = s
    INFO: 2014-03-04 16:48:10.875 (UTC): slotProcessIncomingRawData : incomingMessage = <



  • QTcpSocket is buffered. there is no need to buffer it.
    only use bytesAvailable() for received size and peek for monitoring your received header bytes.
    use setReadBufferSize for setting internal buffer size.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.