Boost logo

Boost :

Subject: Re: [boost] [asio] how to limit speed of tcp socket on asio?
From: Joseph Van Riper (fleeb.fantastique_at_[hidden])
Date: 2013-08-19 09:38:33


On Mon, Aug 19, 2013 at 7:20 AM, Renato Forti <re.tf_at_[hidden]> wrote:

>
> I want know if have some way to limit speed of tcp socket on asio?
>
> For sample if my app open a socket and start to send a file to my server!
> How to limit upload speed?
>
> If I want limit to 100kb/s, have any way? Anyone have any idea: how I can
> achieves this?
>

I know of no way built into asio to do this, nor could I imagine an easy
way for them to implement such a thing, when the need for this sort of
feature might depend upon a variety of factors.

If I were to implement a way to transmit a file over TCP/IP using asio, I
might consider only sending a defined number of bytes per async_write. I
would then use a timer to wait for however long I want to limit the output
before indicating that the write finished in the write handler. When the
timer finished, I'd then signal to send the next packet.

Consider that you aren't going to sending literally one byte at a time over
TCP/IP at a slower rate, but packets of them at a slower rate.

If you want to be hyper-concerned about efficiency, for IPv4, you might see
if you could determine the number of bytes in a TCP/IP frame (the MTU), and
send multiples of that at a time. I do not know, though, how you would go
about finding that value in asio. In Windows, you'd use the socket's
SO_MAX_MSG_SIZE option to obtain the value. On a POSIX operating system, I
think you'd use the socket's IP_MTU option. You might be able to use
boost::asio::ip::tcp::socket::get_option to obtain this information in
asio, but you'll need to know which OS you're on to do it properly.

- Trey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk