Вопрос

I'm trying to write a dynamic data to a QTcpSocket, this is how I implement it:

    connect(&m_tcpSocket, SIGNAL(bytesWritten(qint64)), SLOT(written(qint64)));
//...
void MyClass::written(qint64 iBytes)
{
    if(iBytes>0)
        m_strWrite = m_strWrite.mid(iBytes);
    if(m_strWrite.length()<1)
    {
        if(m_hHandle->isDone())
            m_tcpSocket.disconnectFromHost();
    }else if(m_tcpSocket.isValid()){
        m_tcpSocket.write(m_strWrite);
    }
}
//...
void MyClass::dataReady(const QByteArray &strData)
{
    bool bWrite = m_strWrite.isEmpty();
    m_strWrite.append(strData);
    if(bWrite)
        written(0);
}

dataReady is a slot which is called whenever there is some data ready, and strData is at most 8192 bytes.

This method works perfectly, but when data is huge (>500 MB), strange things happen, sometimes data is written much more than what I expect, sometimes some data is missing, sometimes nothing is written after a while and ...

I just want dynamic buffer, be written to socket, is there another way for that?

Это было полезно?

Решение

QTcpSocket has its own write buffer. Just use m_tcpSocket.write(strData). Qt does not limit the write buffer size.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top