PDA

View Full Version : QTcpSocket and QProgressBar/QProgressDialog



nikkadim
17th September 2012, 23:49
Hello, could you please clarify. I have QTcpServer class who's send the data, and QTcpSocket class who's received this data.

I have a slot slotReadyRead() for ReadReady() signal like this:

void MyClient::slotReadyRead()
{
QDataStream in(m_pTcpSocket);
in.setVersion(QDataStream::Qt_4_8);
for (;;) {
if (!m_nNextBlockSize) {
if (m_pTcpSocket->bytesAvailable() < (int)sizeof(quint64)) {
break;
}
in >> m_nNextBlockSize;
}

if (m_pTcpSocket->bytesAvailable() < m_nNextBlockSize) {
break;
}
in >> bList; //QList
m_nNextBlockSize = 0;
}
}
and I wondering how I can get the values of received date for update some progress indicator?
And second question, how it possible to increase the speed of transmitting data via QTcpSocket?

Thank you.

wysota
18th September 2012, 09:57
and I wondering how I can get the values of received date for update some progress indicator?
You need to know how much data you expect to receive in total. Then when reading the data, calculate how much you already received and set those values on a progress bar.


And second question, how it possible to increase the speed of transmitting data via QTcpSocket?
Use something smarter than QDataStream. This is a really lame approach in terms of efficiency.

nikkadim
18th September 2012, 15:05
You need to know how much data you expect to receive in total. Then when reading the data, calculate how much you already received and set those values on a progress bar.


Use something smarter than QDataStream. This is a really lame approach in terms of efficiency.

Yes, theoretically I understand how I need to calculate, but my question was how!? May be some signals?
I'm afraid I have to use QDataStream because I need to transfer complicated data structure (QList) via socket, and for this small app no reason to make my own functions.
I think the speed issue did not correlate with QDataStream, my be with some settings, memory pre-allocation but not with QDataStream itself.

wysota
18th September 2012, 16:51
Yes, theoretically I understand how I need to calculate, but my question was how!? May be some signals?
It depends how you do the reading. The most basic aparatus you have is bytesAvailable().


I'm afraid I have to use QDataStream because I need to transfer complicated data structure (QList) via socket, and for this small app no reason to make my own functions.
You don't need QDataStream for that.


I think the speed issue did not correlate with QDataStream, my be with some settings, memory pre-allocation but not with QDataStream itself.
Data transfer time over network is directly proportional to the amount of data being transfered. The less data to transfer, the less time it takes.

nikkadim
18th September 2012, 17:10
It depends how you do the reading. The most basic aparatus you have is bytesAvailable().


You don't need QDataStream for that.



Do you have link to some code example for this?
PS: Now I have receiving speed about 20 Mbit/s, at 1Gbit/s NIC. Test was performed on one machine.

wysota
18th September 2012, 20:28
Do you have link to some code example for this?

No, I don't have link to any example code :) If you want an efficient protocol, it has to depend on what you are currently doing.

The most trivial (but probably not a very clever) approach would be to simply memcpy() your structure to the socket and then do the same on the other end.


PS: Now I have receiving speed about 20 Mbit/s, at 1Gbit/s NIC. Test was performed on one machine.
I don't see how that's relevant to anything. Especially if you are sending to localhost which doesn't even use your network interface card.

Either you have efficiency or ease of use. Choose which is more important for you.

nikkadim
18th September 2012, 20:33
Looks like you recommend me don't use QT at all :)

wysota
20th September 2012, 10:42
No, I recommend you to use tools and mechanisms proper to the problem you are facing. I understand you took the code from some Qt example (e.g. fortune cookie) but this is very generic (and badly written) code that is not suited for problems more complex than reading a couple of bytes from the socket. If you know what structure of data you expect, then you really don't need to send the size of that structure to the socket and you don't need to go through generic serialization code that sometimes bloats the data sent unnecessarily (and obscures access to the data from non-Qt applications).

Robbie
20th September 2012, 12:47
I wondering how I can get the values of received date for update some progress indicator?

You've got everything you need for this. You can create a signal like one from QFtp:


void dataTransferProgress(qint64 done, qint64 total);

and then just emit the data you have: bytesAvailable() tells you how much data you have in the internal buffer; m_nNextBlockSize contains the data size you're expecting.