Lucas Rey Lucas Rey - 1 year ago 51
Perl Question

Limit Socket to send specific ammount of packets every second in Perl

I'm programming a simple script in perl that do the following:

  1. Open a socket to a specific server

  2. Reads data line by line from a big file using a while loop

  3. Send data using the open socket

I don't report the whole code since it works perfect and could be too long. What I need to accomplish is send an ammount of packets every seconds. For example I would like to limit to send 100 packets per second. This because the loop is so fast and sometime the server goes in congestion.
What I need, is undestand how (and if) I can implement this kind of mechanism. Is not clear for me how to insert code into the while loop to send "X" packets every second. The actual code looks like:

Open Socket
Open file containing data to send
while # Read file line by line
Send data to Server

Thank you, Lucas.

Answer Source

You might want to check WWW::Curl and use CURLOPT_MAX_SEND_SPEED_LARGE to throttle output traffic.