How can I slow down a TCP connection in Windows?

I am developing a Windows proxy program in which two of my TCP sockets connected through different adapters are connected by my program. That is, my program reads from one socket and writes to another, and vice versa. Each socket is processed by its own thread. When one socket reads data, it is queued for another socket to write it. The problem I have is the case when one link works at a speed of 100 MB and the other at 10 MB. I read data from a 100 MB channel faster than I can write it to a 10 MB link. How can I โ€œslow downโ€ a faster connection so that it essentially runs at a slower connection speed? Changing a faster link to a slower speed is not an option. --Thanks

+4
source share
5 answers

Create a fixed queue length between reading and writing streams. Block the queue in the queue when the queue is full, and when deactivated, when it is empty. A regular semaphore or mutex / condition variable should work. Play with queue size, so slow flow is always busy.

+8
source

If this is a problem, you are writing your program incorrectly.

You cannot set more than 10 Mbit / s to a 10 Mbit / s link, so the stream that is written to the slower link should start to block when recording. As long as your stream uses a read buffer of the same size as the write buffer, the stream should only consume data as fast as it can throw it out of the 10 Mbps pipe. Any flow control necessary for a remote sender to put you more than 10 Mbps into a 100 Mbps tube will automatically depend on the TCP protocol.

So this should not be a problem if your read and write buffers are the same size in this stream (or any stream).

+6
source

Stop reading data when you cannot write it.

Your program includes a queue of bytes from a 100 Mbps link and a queue from your program to a 10 Mbps link. When the outgoing queue is full, stop reading from the incoming queue and TCP with the throttle back to the client at the 100 Mbps link.

You can use the internal queue between the reader and the author to implement this cleanly.

+4
source

Many difficult and correct decisions were made. But in fact, to understand the essence of the question - why do you have two topics? If you read socket-100, socket-10 will write in one stream, it will, of course, be blocked during writing, and you will not need to design anything complicated.

+3
source

If you are running a non-blocking select () loop - style: just call FD_SET (readSocket and readSet) if your outgoing queue is less than some hard size.

Thus, when the outgoing socket lags, your proxy will stop reading data from a faster client until it picks up a backup. TCP will take care of the rest (in particular, it will say that your faster client slows down for a while)

0
source

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


All Articles