What is the behavior of QTcpSocket when the network is congested?

I have a QTcpSocket in a separate thread sending a lot of data. Some of these applications are distributed over the network.

How can I determine if my network is congested and my socket cannot send anything else? Will QTcpSocket buffer all data, how can I see the size of my buffered data that will be sent to the queue? Will this be equal to bytesToWrite ()? Is there a maximum bytesToWrite ()?

If QTcpSocket starts discarding the data, what will it be: the oldest in the buffer queue, the newest in the buffer queue, ...?

+3
source share
3 answers

Apparently this is not the case, I just checked the code for Qt 4.5.3. When write () is called, all data is placed in the QRingBuffer (the class is not part of the Qt API). QAbstractData :: writeData () puts the data in, QAbstractData :: flush () pulls it. There is no check that limits the size of this buffer. The size returned by QAbstractSocket :: write () is always the size of your data, or -1 on error.

+2
source

QTcpSocket buffers everything you write in it and tries to write as much as possible the next time the event loop is run. You should do your own โ€œflow controlโ€ by checking socket-> bytesToWrite (), which tells you how much more is in the Qt buffer (+ additional data will be added to your OS buffer that you do not control)

+1
source

You should check bytesToWrite () yourself and discard any old / new data if it reaches its maximum:

if (bytesToWrite() > 16 * 1024 * 1024) { // discard data } else { write(moredata); } 

bytesToWrite () is an indicator of how fast it can be sent. You can check this value and throttle the datarate so that bytesToWrite () always returns about the same size.

0
source

Source: https://habr.com/ru/post/1347053/


All Articles