I am transferring a file from a PC server written in Qt to the android client. The client accepts the first couple of byte packets normally. And then the hellish lag from the server begins. Those. in the client almost all the time in.available() == 0 .
Previously, there was no such wild lag, the code seemed to be the same. flush returns true , and the client hangs in a loop with available=0 .
Internet speed on the client seems to be the same as on the PC, even more.

Sending a file briefly:

 int len = file.size(); int writingBytes0 = 0; if (len > 8192) { while (1) { QByteArray b = file.read(8192); if (b.isEmpty()) { pClientSocket->flush(); break; } pClientSocket->write(b); bool fl = pClientSocket->flush(); writingBytes0 += b.size(); mw->setPrUn(writingBytes0); } } else { QByteArray b = file.read(len); //stream->writeRawData(b, b.size()); pClientSocket->write(b); mw->setPrUn(len); pClientSocket->flush(); } file.close(); 

The file acceptance code on the client:

 long msize = file.length(); long writebites = 0; long writebites0 = msize; while (msize != writebites) { int avaibl = in.available(); if (avaibl > 0) { //вот тут отставание, и очень сильное if (avaibl < writebites0) { byte[] buf = new byte[avaibl]; in.read(buf); writebites = writebites + avaibl; fos.write(buf); writebites0 = writebites0 - avaibl; } else { byte[] buf = new byte[(int)msize]; in.read(buf); writebites = msize; fos.write(buf); writebites0 = 0; } } } 
  • I would start to start wireshark and look. And what is the "hell lag"? second? minute? day? - KoVadim
  • Approximately a second. - Madoka Magica
  • I am very confused by this endless cycle. Is he accidentally running in the main thread? - KoVadim
  • In Qt, yes, I later thought to transfer to a separate thread, then it worked. - Madoka Magica
  • just if it's all spinning in the main thread, then the eventloop is blocked with all the ensuing consequences. - KoVadim

1 answer 1

Transferring to a separate thread did not help me, it helped that instead of

 pClientSocket->flush(); 

began to use

 pClientSocket->waitForBytesWritten(); 

In principle, the documentation itself trolltech advises to use it. The truth is slower shipping, but there is no lag in the client.