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. Cannot read complete data, bytes available != actual bytes read
QtWS25 Last Chance

Cannot read complete data, bytes available != actual bytes read

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.0k 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.
  • K Offline
    K Offline
    Kavita123
    wrote on last edited by
    #1

    Hello All,

    I am a newbie to QT. I am developing an application that is using QTCPSocket.
    I have created a signal for reading as below:
    @
    connect(m_pSocket,SIGNAL(readyRead()),SLOT(SocketDataReceived()));
    @

    and code to read data is:
    @
    void ABC::SocketDataReceived()
    {
    int nBytesAvaialble=m_pSocket->bytesAvailable();
    LOGDEBUG(CM,"SocketDataReceived : bytes Available initialially = " << nBytesAvaialble);

    QString m_qstrNextMesssage;
    
    while (nBytesAvaialble > 0)
    {
        m_qstrNextMesssage.append(m_pSocket->read(nBytesAvaialble));
        nBytesAvaialble = m_pSocket->bytesAvailable();
    }
    
    if(m_qstrNextMesssage.length() <= 0 )
    {
        LOGWARN(CM, "No data is read in this read API");
        return;
    }
    LOGDEBUG(CM, "Bytes read = " << m_qstrNextMesssage.length());
    LOGDEBUG(CM, m_qstrNextMesssage.toLatin1().data());
    emit DataReceived(m_qstrNextMesssage);
    m_qstrNextMesssage.clear();
    

    }
    @

    But when I run this code, the log I am getting shows that available bytes are not equal to bytes read (I tried using read() and then readAll() APIs).
    @
    [01/16/2015 14:53:05:801] [15976] [DEBUG] [CM] SocketDataReceived : bytes Available initialially = 412
    [01/16/2015 14:53:05:801] [15976] [DEBUG] [CM] Bytes read = 145

    Received data of length 145

    [01/16/2015 14:53:06:274] [15976] [DEBUG] [CommunicationManager] SocketDataReceived : bytes Available initialially = 146
    [01/16/2015 14:53:06:294] [15976] [DEBUG] [CommunicationManager] Bytes read = 145

    Received data of length 145
    @

    So, in 1st read call bytes available = 412, actual bytes read = 145
    in 2nd read call bytes available = 146, actual bytes read = 145

    So, where are 412-145 = 267 ? they should be available in next read call right ? So in next read should show bytes available as atleast 267.

    What is causing the loss of data here ? Please help.

    -Thanks,
    Kavita

    [Edited - Please use code tags "@@" - p3c0]

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      First of all, when placing code please use the @ before and @ after the snippets. Then the forum will display them as if real code.
      The we might be able to support you.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        i don't know which data you are receiving on the socket, but count the read bytes directly and not from the string. Try this:
        @
        qint64 nBytesAvaialble=m_pSocket->bytesAvailable(); //<-- not using qint64 may cause possible loss of int-data here
        ...
        QByteArray readData = m_pSocket->read(nBytesAvaialble); // or use readAll()
        qint64 readBytes = readData.size();
        m_qstrNextMesssage.append(readData);
        @

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0

        • Login

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