Mon programme étant court, je me permets de l'inclure ici. J'ai codé ça vite fait car je voulais faire des transferts en limitant la bande passante entre chez moi et le boulot (plutôt pour épargner le boulot). Le formattage ne va pas être beau, mais Templeet ne permet pas je crois les balises PRE.
/* cc -o throttle -g -Wall throttle.c */
/* reads stdin and send it to stdout at a specified rate */
int main(int argc, char **argv)
{
int myspeed;
char mybuf[MYBUFSIZE];
ssize_t len1;
struct timeval tv1, tv2;
int sec1, usec1;
double interval1, time1;
unsigned long sleep1;
if (argc < 2) {
fprintf(stderr, "%s: usage: throttle speed_in_Bps\n", argv[0]);
exit(1);
}
myspeed = atoi(argv[1]);
if (myspeed < 1 || myspeed > 10000000) {
fprintf(stderr, "%s: speed_in_Bps must be in [1..10000000]\n", argv[0]);
exit(1);
}
/* init of last timestamp */
tv1.tv_usec = tv1.tv_sec = 0;
/* compute time interval between packets to respect speed */
interval1 = (double) MYBUFSIZE / (double) myspeed;
while (1) {
/* fill the read buffer (or try to if near eof) */
len1 = read(STDIN_FILENO, mybuf, MYBUFSIZE);
if (len1 == -1) {
perror("throttle reading stdin");
exit(1);
}
if (len1 == 0) { /* end of file, we quit normally */
break;
}
/* take current time */
gettimeofday(&tv2, NULL);
/* compute difference */
usec1 = tv2.tv_usec - tv1.tv_usec;
sec1 = tv2.tv_sec - tv1.tv_sec;
if (usec1 < 0) { usec1 += (1000 * 1000); sec1--; }
time1 = (double) sec1 + (double) usec1 / 1000000.0;
if (time1 < interval1) {
/* we have to wait */
sleep1 = (interval1 - time1) * 1000000;
usleep(sleep1);
}
/* take current time for last timestamp */
gettimeofday(&tv1, NULL);
# exemple de code pour limiter le débit
Posté par Olivier Jeannet . En réponse au message Limitation de bande passante. Évalué à 3.
/* cc -o throttle -g -Wall throttle.c */
/* reads stdin and send it to stdout at a specified rate */
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#define MYBUFSIZE 512
int main(int argc, char **argv)
{
int myspeed;
char mybuf[MYBUFSIZE];
ssize_t len1;
struct timeval tv1, tv2;
int sec1, usec1;
double interval1, time1;
unsigned long sleep1;
if (argc < 2) {
fprintf(stderr, "%s: usage: throttle speed_in_Bps\n", argv[0]);
exit(1);
}
myspeed = atoi(argv[1]);
if (myspeed < 1 || myspeed > 10000000) {
fprintf(stderr, "%s: speed_in_Bps must be in [1..10000000]\n", argv[0]);
exit(1);
}
/* init of last timestamp */
tv1.tv_usec = tv1.tv_sec = 0;
/* compute time interval between packets to respect speed */
interval1 = (double) MYBUFSIZE / (double) myspeed;
while (1) {
/* fill the read buffer (or try to if near eof) */
len1 = read(STDIN_FILENO, mybuf, MYBUFSIZE);
if (len1 == -1) {
perror("throttle reading stdin");
exit(1);
}
if (len1 == 0) { /* end of file, we quit normally */
break;
}
/* take current time */
gettimeofday(&tv2, NULL);
/* compute difference */
usec1 = tv2.tv_usec - tv1.tv_usec;
sec1 = tv2.tv_sec - tv1.tv_sec;
if (usec1 < 0) { usec1 += (1000 * 1000); sec1--; }
time1 = (double) sec1 + (double) usec1 / 1000000.0;
if (time1 < interval1) {
/* we have to wait */
sleep1 = (interval1 - time1) * 1000000;
usleep(sleep1);
}
/* take current time for last timestamp */
gettimeofday(&tv1, NULL);
/* write data */
len1 = write(STDOUT_FILENO, mybuf, len1);
if (len1 == -1) {
perror("throttle writing stdout");
exit(1);
}
} /* while(1) */
return 0;
}