简体   繁体   English

如何减慢Windows上的TCP连接速度?

[英]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. 我正在开发一个Windows代理程序,其中两个通过不同适配器连接的TCP套接字由我的程序桥接。 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. 我遇到的问题是当一个链接以100Mb运行而另一个链接以10Mb运行时。 I read data from the 100Mb link faster than I can write it to the 10Mb link. 我从100Mb链接读取数据的速度比我写入10Mb链接的速度快。 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 - 谢谢

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. 使用队列大小播放,因此较慢的线程始终处于忙碌状态。

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. 您不能在10mbps链接上放置超过10mbps,因此在较慢的链接上写入的线程应该在您编写时开始阻塞。 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. 因此,只要您的线程使用与写入缓冲区相同大小的读取缓冲区,线程就应该尽可能快地使用数据,因为它可以将数据丢回到10mbps管道中。 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. 任何流量控制都需要保持远程发送器不超过10mbps到100mbps管道中,TCP协议会自动处理。

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). 所以只要你的读写缓冲区在该线程(或任何线程)中的大小相同,它就不应该成为问题。

Stop reading the data when you are not able to write it. 当您无法写入数据时停止读取数据。

There is a queue of bytes coming into your program from the 100Mb/s link, and a queue out of your program to the 10Mb/s link. 从100Mb / s链接进入程序的字节队列,以及从程序到10Mb / s链接的队列。 When the outgoing queue is full, stop reading from the incoming queue and TCP with throttle back the client on the 100Mb/s link. 当传出队列已满时,停止从传入队列和TCP中读取,并在100Mb / s链路上使用节流功能返回客户端。

You can use an internal queue between the reader and the writer to implement this cleanly. 您可以使用阅读器和编写器之间的内部队列来干净地实现此目的。

A lot of complicated - and correct - solutions have been expounded. 阐述了许多复杂而正确的解决方案。 But really, to get to the crux of the matter - why do you have two threads? 但实际上,要解决问题的关键 - 为什么你有两个线程? If you did the socket-100 read, socket-10 write in a single thread, it would naturally block on the write and you wouldn't have to design anything complicated. 如果你在一个线程中执行了socket-100读取,socket-10写入,它自然会阻止写入,你不必设计任何复杂的东西。

If you are doing a non-blocking, select()-style event loop: only call FD_SET(readSocket, &readSet) if your outgoing-data queue is smaller than some hard-coded maximum size. 如果您正在进行非阻塞,请选择() - 样式事件循环:如果您的传出数据队列小于某个硬编码的最大大小,则只调用FD_SET(readSocket,&readSet)。

That way, when the outgoing socket falls behind, your proxy will stop reading data from the faster client until it catches back up. 这样,当传出套接字落后时,您的代理将停止从更快的客户端读取数据,直到它重新启动。 The TCP protocol will take care of the rest (in particular, it will tell your faster client to slow down for a while) TCP协议将负责其余的工作(特别是,它将告诉您的更快的客户端减速一段时间)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM