[Qt-interest] Socket communication without threads

Roland Krause rokrau at yahoo.com
Tue Mar 3 18:35:54 CET 2009


There is an additional problem here with the block size at least for QLocalSocket. I've been experimenting with it and noticed that if you send a large block of data at a time, you have to manually process events in order to receive all the data in one go. It seems that the readyRead signal is emitted and when you call read(char * data, quint64  size) you can only get as much data as was in the buffer at the time of the call which must not necessarily be all the data that was written. 

In my case I am trying to send and receive "data records" between two programs. Each record has header and footer and payload which can be hundreds of megs in size. My first humble attempts showed that I can not keep receiving in a loop after a readyRead() signal, I could only get what's available at the time of the corresponding read call at a max of 128k. Then I either have to manually process events and the buffer is filled further or return and wait for another readyRead() signal. The latter breaks up the data flow and I have to keep track of state of the reader (i.e. was I reading header, footer, payload, which record etc..). 

I am going to try the same with QTcpSocket next and see whether the problem is the same there (it should afaict from looking at the implementation of QLocalSocket).

Anyway, I believe for large amounts of payload data a threaded implementation could be warranted.

Regards
Roland

PS: Here is a short blurb of my function that is called after a  readyRead() signal

QByteArray readRecordFromSocket(QLocalSocket * pSocket)
{
    QByteArray empty;
    if (!pSocket) return empty;
    if (pSocket->isValid() &&
         (pSocket->state()==QLocalSocket::ConnectedState)) {

        QDataStream ds(pSocket);
        int record_len;
        ds >> record_len;
        int element_size;
        ds >> element_size;
        QByteArray ba(record_len-12);
        //
        int sleepTime=0;
        while (pSocket->bytesAvailable()<ba.size()) {
            qDebug() << "\t\tpSocket->bytesAvailable()=" << pSocket->bytesAvailable();
            qApp->processEvents();
            usleep(100);
            sleepTime+=100;
            if (sleepTime==5000) break;
        }
        if ((sleepTime==5000)&&(pSocket->bytesAvailable()<ba.size())) {
            qDebug() << "::readFromSocket(), read timeout after 5000 ms:\n"
                     << "trying to read " << ba.size() << "bytes but only" << pSocket->bytesAvailable()
                     << "are available";
            return empty;
        }
        ds.readRawData(ba.data(),ba.size());
        int check_word;
        ds >> check_word;
        qDebug() << "::readFromSocket(), read:\n"
                 << "record_len=" << record_len
                 << "element_size=" << element_size
                 << "array=" << ba[0] << ba[1] << ba[2] << ba[3]
                 << "check_word=" << check_word ;
        return ba;
    }
    return empty;
}





On Mon March 2 2009 20:27:43 Predrag Manojlovic wrote:
> What is bad there?
If you do it right, using QThread isn't "bad" at all, but sending huge amounts 
of data at once to one client from one of those threads is not a good idea, 
especially not when you're doning this with a larger number of parallel 
threads.
-- 


      



More information about the Qt-interest-old mailing list