Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I slow down a TCP connection on Windows?

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

like image 363
meg18019 Avatar asked Nov 29 '22 18:11

meg18019


2 Answers

Create a fixed length queue between reading and writing threads. Block on the enqueue when queue is full and on dequeue when it's empty. Regular semaphore or mutex/condition variable should work. Play with the queue size so the slower thread is always busy.

like image 110
Nikolai Fetissov Avatar answered Dec 04 '22 15:12

Nikolai Fetissov


If this is a problem, then you're writing your program incorrectly.

You can't put more than 10mbps on a 10mbps link, so your thread that is writing on the slower link should start to block as you write. So as long as your thread uses the same size read buffer as write buffer, the thread should only consume data as quickly as it can throw it back out the 10mbps pipe. Any flow control needed to keep the remote sender from putting more than 10mbps into the 100mbps pipe to you will be taken care of automatically by the TCP protocol.

So it just shouldn't be an issue as long as your read and write buffers are the same size in that thread (or any thread).

like image 36
Southern Hospitality Avatar answered Dec 04 '22 14:12

Southern Hospitality