1

I have a server application written in c++, which is sending data to socket (and client is reading it). I need to restrict "upload speed" to that socket. Is there any easy way to do this? I have a number which respresents number of kB/s and that should be the upload speed.

I am using:

#include <sys/types.h>
#include <sys/socket.h>

ssize_t send(int sockfd, const void *buf, size_t len, int flags);
dominique
  • 11
  • 1
  • See http://stackoverflow.com/questions/10628472/how-do-i-limit-socket-speed-in-c – Mihai8 May 03 '14 at 16:27
  • If the server is sending , this is a download, not an upload. I'm wondering why you want to do this. Normally the server should send as fast as possible to get rid of the client and all the resources it requires at the server as fast as possible. The network will throttle itself as necessary. – user207421 May 03 '14 at 17:33
  • I need to do this, because it's school assignement. I managed to send files via socket, but I need to restrict the speed server is sending data to socket. – dominique May 03 '14 at 19:10

1 Answers1

4

How about starting with the following simplistic approach?

  • Maintain a send buffer, per destination, to accumulate the bytes to send

  • Setup a 1 second recurring timer

  • On timer expiry, send at most the specified bytes from the buffer, for every destination

For a smarter sophisticated solution, the sleep time need to be dynamically computed based on the send history.

Arun
  • 19,750
  • 10
  • 51
  • 60
  • Check out the linked duplicate. This is not an optimal solution. – Jonathon Reinhart May 03 '14 at 16:25
  • But that would also influence the delay, which might not be an option in this guys project – user2520938 May 03 '14 at 16:25
  • @JonathonReinhart: Agreed. That is why I mentioned that it is a simplistic approach just for starting. I made them boldface so that the reader understands that this is basic idea, but for a real life production quality application, many other factors need to be considered. – Arun May 03 '14 at 16:29