We have two Qt applications. App1 accepts the connection from App2 through QTcpServer and stores it in the QTcpSocket* tcpSocket . App1 starts the simulation with a frequency of 30 Hz. For each simulation run, a several kilobyte QByteArray sent using the following code (from the main / graphic stream):
QByteArray block; tcpSocket->write(block, block.size()); tcpSocket->waitForBytesWritten(1);
The receiver socket listens for the QTcpSocket :: readDataBlock signal (in the main / GUI stream) and prints the corresponding timestamp in the GUI.
When both App1 and App2 are running on the same system, the packages synchronize perfectly. However, when App1 and App2 run on different systems connected through the network, App2 no longer synchronizes with the simulation in App2. Packages go much slower. Even more surprisingly (and indicates that our implementation is incorrect) is that when we stop the simulation cycle, no more packets are received. This surprises us because we expect from the TCP protocol that all packets will come eventually.
We built a TCP logic based on Qt good luck example . However, a successful server is different because it sends only one packet to an incoming client. Can someone determine what we did wrong?
Note: we use MSVC2012 (App1), MSVC2010 (App2) and Qt 5.2.
Edit: With a package, I mean the result of one simulation experiment, which is a bunch of numbers written in a QByteArray block . However, the first bits contain the length of the QByteArray, so the client can check if all the data has been received. This is the code that gets called when the QTcpSocket :: readDataBlock signal is issued:
QDataStream in(tcpSocket); in.setVersion(QDataStream::Qt_5_2); if (blockSize == 0) { if (tcpSocket->bytesAvailable() < (int)sizeof(quint16)) return; // cannot yet read size from data block in >> blockSize; // read data size for data block } // if the whole data block is not yet received, ignore it if (tcpSocket->bytesAvailable() < blockSize) return; // if we get here, the whole object is available to parse QByteArray object; in >> object; blockSize = 0; // reset blockSize for handling the next package return;
source share