views:

88

answers:

3

I'm using raw sockets on windows and I'm trying to find a way to limit the max connection speed over a group of sockets.

For example I have 3 sockets to 3 servers and want to limit total download speed to 1mb.

I googled and cant find any thing related. Any ideas?

+3  A: 

If you want to limit the download speed to 1 MB per second, manage your recv() calls in such a way that you do not recv() more than 1 MB in a single second. Once you have read the maximum 1 MB, throttle the thread (using ThreadSleep) until the next second. That's just a simple approach.

Will
One of the problems with this is that you probably want a small enough packet size to go fast enough through the loop to be able to sleep. But it kind of slows everything down more.
jpyllman
+1  A: 

I think the only way to implement this is the hard way... by measuring the amount of data that has been sent/received on each socket in the last (so many) seconds, and holding off on sending/receiving more data until enough time has passed to limit the total average bandwidth to what you want it to be. I've implemented this logic using non-blocking sockets and select(), and it's doable (albeit not easy).

Jeremy Friesner
+1  A: 

This is like asking "I want to read half as fast from my hard drive". Or "I want to use only 50% of the CPU cycles". Doesn't make sense, it goes as fast as it can. If more programs compete for the resource then it is up to the operating system to ensure everybody gets a fair share. If you don't want a fair share then simply lower the thread priority of your thread doing the reading. Or call Sleep().

Hans Passant
+1 for letting the 'system' take care of the load. And most of the time giving the one who can deliver fast full throttle make things finish faster. But I guess sometimes you need to be nice to you video feeds.
jpyllman
well the issue is that its currently taking up all the users download bandwidth so would be nice to allow them to limit it.
Lodle