[Solved] QTcpSocket connection data received is empty after certain limit



  • HI,

    I am trying to send data between server and client using qtcpsocket connection. When I send small size data everything is fine. But when size increases the data read from tcpsocket goes empty :(.

    // data to be sent

    @int ClientService::syncMasters(SyncMaster syncMasterData)
    {
    qDebug() << Q_FUNC_INFO << "Invoked";

    int syncStatus = -1;
    
    if(enableConnection())
    {
        qDebug() << Q_FUNC_INFO << "connection enabled";
        //  typecast to ensure cross platform compatibility
        const qint32 mode_int = static_cast<qint32>(SYNC_MASTERS_QUERY);
        QByteArray block;
        QDataStream outputStream(&block, QIODevice::WriteOnly);
        outputStream.setVersion(QDataStream::Qt_4_0);
        outputStream << (quint16)0;
        outputStream << mode_int;       // saying query mode to server
    
        // group master data
        outputStream << syncMasterData.groupMasterList.count();
        qDebug() <<  "groupmaster count" <<
                                    syncMasterData.groupMasterList.count();
    
        foreach (GroupMaster groupMasterItem, syncMasterData.groupMasterList)
        {
            outputStream << groupMasterItem.groupCode;
            outputStream << groupMasterItem.groupDescription;
            outputStream << groupMasterItem.kotPrinterNo;
            outputStream << groupMasterItem.image;
            outputStream << groupMasterItem.sortId;
        }
    
        // subgroup master data
        outputStream << syncMasterData.subGroupMasterList.count();
        qDebug() << Q_FUNC_INFO << "subgroupmaster count" <<
                                    syncMasterData.subGroupMasterList.count();
    
        foreach (SubGroupMaster subGroupMasterItem,
                                syncMasterData.subGroupMasterList)
        {
            outputStream << subGroupMasterItem.groupCode;
            outputStream << subGroupMasterItem.subGroupCode;
            outputStream << subGroupMasterItem.subGroupDescription;
    
        }
    
        // item master data
        outputStream << syncMasterData.itemMasterList.count();
        qDebug() << Q_FUNC_INFO << "item master count" <<
                                    syncMasterData.itemMasterList.count();
    
        foreach (ItemMaster ItemMasterItem, syncMasterData.itemMasterList)
        {
            outputStream << ItemMasterItem.itemCode;
            outputStream << ItemMasterItem.itemName;
            outputStream << ItemMasterItem.groupCode;
            outputStream << ItemMasterItem.subGroupCode;
            outputStream << ItemMasterItem.imagePath;
            outputStream << ItemMasterItem.productId;
            outputStream << ItemMasterItem.itemPriceType;
            outputStream << ItemMasterItem.itemPrice;
            outputStream << ItemMasterItem.kotPrinterNumber;
            outputStream << ItemMasterItem.sortId;
        }
    
        // side item master data
        outputStream << syncMasterData.sideItemMasterList.count();
        .
        .
        . ... // similar data is sent
        
        outputStream.device()->seek(0);
        outputStream << (quint16)(block.size() - sizeof(quint16));
    
        if(sendToServer(block))
        {
            qDebug() << Q_FUNC_INFO << "query sent to server successfully";
    
            if (waitForServerResponse())
            {
                qDebug() << Q_FUNC_INFO << "read set status from server";
    
                QDataStream inputStream(&m_socket);
                inputStream.setVersion(QDataStream::Qt_4_0);
                inputStream >> syncStatus;
            }
        }
    
        disableConnection();
    }
    
    qDebug() << Q_FUNC_INFO << "Exits" << syncStatus;
    return syncStatus;
    

    }@

    // read is also similar

    @void ServerFunctionThread::syncMasters()
    {
    qDebug() << Q_FUNC_INFO << "Invoked";

    SyncMaster syncMasterData;
    
    int groupCount      = 0,
    subGroupCount   = 0,
    itemCount       = 0,
    sideItemCount   = 0,
    priceCount      = 0;
    
    QDataStream inputStream(&tcpSocket);
    inputStream.setVersion(QDataStream::Qt_4_0);
    
    // group master data reception
    inputStream >> groupCount;
    qDebug() << Q_FUNC_INFO << "group master Count" << groupCount;
    
    while (groupCount--)
    {
        GroupMaster groupMasterItem;
        inputStream >> groupMasterItem.groupCode;
        inputStream >> groupMasterItem.groupDescription;
        inputStream >> groupMasterItem.kotPrinterNo;
        inputStream >> groupMasterItem.image;
        inputStream >> groupMasterItem.sortId;
    
        syncMasterData.groupMasterList.append(groupMasterItem);
    
        qDebug() << Q_FUNC_INFO <<
        "code"      << groupMasterItem.groupCode         <<
        "desc"      << groupMasterItem.groupDescription  <<
        "kot "      << groupMasterItem.kotPrinterNo      <<
        "image"     << groupMasterItem.image             <<
        "sort id"   << groupMasterItem.sortId;
    
    }@
    

    .

    Can someone help me to know if there a size limit to the data written into QDataStream...
    Because from my logs connection is still alive and i get response. But data received is empty after few data is read.



  • // enabling connection
    @bool ClientService::enableConnection()
    {
    const int iTimeout = 5 * 1000;
    m_socket.connectToHost(settings.getServerIP(), PORT);
    if (!m_socket.waitForConnected(iTimeout))
    {
    emit error();
    qDebug() << m_socket.errorString();
    return false;
    }
    return true;
    }

    // send data to server once all data is written into QDataStream
    bool ClientService::sendToServer(QByteArray block)
    {
    bool sendStatus = false;
    if (QTcpSocket::ConnectedState == m_socket.state())
    {
    if (-1 != m_socket.write(block))
    {
    m_socket.waitForBytesWritten();
    sendStatus = true;
    }
    else
    {}
    }
    else
    {}
    if (!sendStatus)
    {
    emit error();
    }
    return sendStatus;
    }

    // wait until response is received from server

    bool ClientService::waitForServerResponse()
    {
    qDebug() << "Invoked";
    const int iTimeout = 5 * 1000;
    while (m_socket.bytesAvailable() < (int)sizeof(quint16))
    {
    if (!m_socket.waitForReadyRead(iTimeout))
    {
    emit error();
    qDebug() << m_socket.errorString();
    return false;
    }
    }

    quint16 blockSize;
    QDataStream inputStream(&m_socket);
    inputStream.setVersion(QDataStream::Qt_4_0);
    inputStream >> blockSize;

    while (m_socket.bytesAvailable() < blockSize)
    {
    if (!m_socket.waitForReadyRead(iTimeout))
    {
    emit error();
    qDebug() << m_socket.errorString();
    return false;
    }
    }

    return true;
    }@

    // disable the connection once response is received

    @void ClientService::disableConnection()
    {
    qDebug() << "Invoked";
    m_socket.disconnectFromHost();
    qDebug() << "Exits";
    return;
    }@


  • Lifetime Qt Champion

    Hi,

    The thing I see is that you use your socket as a QByteArray which it's not. You should rather read the content of your socket when data arrived in a QByteArray, then once your byte array is of the proper size parse it.

    Hope it helps



  • Thanks SGaist,

    I use QByteArray to create data that has to be send through socket. And at time to send I write this QByteArray to the socket. I follow this as found from qt examples...

    The issue is resolved now. :)

    The data size passed from client to server caused the issue. Since I used quint16 when the size of data becomes more the server doesn't get to know the exact incoming data size. So I changed the type to quint32. And the issue is resolved.

    @outputStream << (quint32)0;@

    // and end

    @outputStream.device()->seek(0);
    outputStream << (quint32)(block.size() - sizeof(quint32));@

    and server side also waits for data size received in quint32


Log in to reply
 

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